这是我的JSON,现在我想找出属性为array或string。

{
    “数据”:[

{

     "name": "my Service",
     "description":"   comes once here  " ,
     "tables":{ "tables":["rajaaaaa ","rajuuuuu","mommmm"]},
     "imgUrl":"../assets/images/nmpm.jpg"

},

 {
     "name": "my Service1",
     "description":"   comes once here  " ,
     "tables":{ "tables":["rajaaaaa ","rajuuuuu","mommmm"]},
     "imgUrl":"../assets/images/nmpm.jpg"
},

 {

     "name": "my Service2",
     "description":"   comes once here  " ,
     "tables":{ "tables":["rajaaaaa ","rajuuuuu","mommmm"]},
     "imgUrl":"../assets/images/nmpm.jpg"
},

 {

     "name": "my Service3",
     "description":"   comes once here  " ,
     "tables":{ "tables":["rajaaaaa ","rajuuuuu","mommmm"]},
     "imgUrl":"../assets/images/nmpm.jpg"
}


]
}

现在如何找出属性是字符串还是数组。

最佳答案

据我了解您的答案,因为您需要确定每种属性类型。
我已经完成了下面的代码。如果我不在你的路线上,请告诉我。我只是安慰所有类型


var data = [

{

     "name": "my Service",
     "description":"   comes once here  " ,
     "tables":{ "tables":["rajaaaaa ","rajuuuuu","mommmm"]},
     "imgUrl":"../assets/images/nmpm.jpg"

},

 {
     "name": "my Service1",
     "description":"   comes once here  " ,
     "tables":{ "tables":["rajaaaaa ","rajuuuuu","mommmm"]},
     "imgUrl":"../assets/images/nmpm.jpg"
},

 {

     "name": "my Service2",
     "description":"   comes once here  " ,
     "tables":{ "tables":["rajaaaaa ","rajuuuuu","mommmm"]},
     "imgUrl":"../assets/images/nmpm.jpg"
},

 {

     "name": "my Service3",
     "description":"   comes once here  " ,
     "tables":{ "tables":["rajaaaaa ","rajuuuuu","mommmm"]},
     "imgUrl":"../assets/images/nmpm.jpg"
}
];


data.forEach(obj=>{

   var keys = Object.keys(obj);
   for(var k=0;k< keys.length; k++){
      if(typeof obj[keys[k]] == 'string'){
           console.log(keys[k]+" is string");
      }else if(typeof obj[keys[k]] == 'object'){


            var obj_keys = Object.keys(obj[keys[k]]);

            for(var j = 0; j< obj_keys.length; j++){


             if(Object.prototype.toString.call( obj[keys[k]][obj_keys[j]] ) === '[object Array]') {
               console.log(keys[k]+" has Array");
             }
            }

      }


   }

});

关于javascript - 如何在JavaScript中找出对象键是字符串还是数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44541898/

10-09 13:55