我有一个包含JSON Object的javascript数组,如下所示: -
var data :[{
name:"ABC",
categories:[{
name:"XYZ",
categories:[{
.
.
.
N
}]
}]
}]
我想在这个数组中使用iterator并想检查categories[]
是否存在而Categories[]
是否有任何值,如果是,则将数据插入其中
EG:
var mydata:[];
mydata.push({name:"MNP",Categories:[]});
我想在类别
中插入此数据答案 0 :(得分:0)
// this is a sample object containing 1 element is text and one element is an array
var object = {smapleA: 'some text', sampleB : ['stringArray']};
// init a var to save the status (intial it must be false)
var containsArray = false;
// go over each element in the object with this loop definition
for(let key in object){
// for be clean check if object.key realy exists
if(object.hasOwnProperty(key){
// here you check if the current element is an Array
if(Array.isArray(object[key])){
// set containsArray variable to true
containsArray = true;
}
}
}
console.log(containsArray);