[英]How to check whether a json contains
I have a javascript array containing JSON Object like this:-我有一个包含 JSON 对象的 javascript 数组,如下所示:-
var data :[{
name:"ABC",
categories:[{
name:"XYZ",
categories:[{
.
.
.
N
}]
}]
}]
I want to iterator in this array and wanna check whether categories[]
present or not and Categories[]
have any value or not , If yes then insert the data inside it我想在这个数组中迭代并想检查
categories[]
是否存在和Categories[]
是否有任何值,如果是,则在其中插入数据
EG: EG:
var mydata:[];
mydata.push({name:"MNP",Categories:[]});
I want to insert this data inside categories我想在类别中插入这些数据
// 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.