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)
typeof item === 'object' && item !== null instead because if the item in the array is null it will use that as the indexformulaArray && to check if the array is nullPlease 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!
{}?