I have an array like below
let formulaArray = ["(", "(", "(", "2", "+", "3", ")", "*", "3", ")", "+", "(", "12", "/",{}]
}
json object in an array can be at any index.Now,I want to find the index of json object ,how can i do that?
I have an array like below
let formulaArray = ["(", "(", "(", "2", "+", "3", ")", "*", "3", ")", "+", "(", "12", "/",{}]
}
json object in an array can be at any index.Now,I want to find the index of json object ,how can i do that?
Use .forEach()
and then check the object with typeof
operator, if true, then push it to an array
var formulaArray = ["(",{}, "(", "(", "2", "+", "3", ")", "*", "3", ")", "+", "(", "12", "/",null,{}]
var objectIndex = [];
formulaArray && formulaArray.forEach(function(item, index){
typeof item==='object' && item!==null &&
objectIndex.push(index);
})
console.log(objectIndex)
Please check out this SO answer https://stackoverflow.com/a/36419269/3902739
It defines the correct structure of an array of Json objects and uses JavaScript functions such as Array.find
or Array.forEach
or Array.filter
as clearly explained in the example provided.
Hope this helps!