如何使用Javascript在数组中查找json对象的索引

时间:2019-01-26 09:10:38

标签: javascript

我有一个像下面的数组

let formulaArray = ["(", "(", "(", "2", "+", "3", ")", "*", "3", ")", "+", "(", "12", "/",{}]
 }

数组中的json对象可以是任何索引。现在,我想找到json对象的索引,我该怎么办?

2 个答案:

答案 0 :(得分:2)

使用.forEach(),然后使用typeof运算符检查对象,如果为true,则将其推入数组

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)

答案 1 :(得分:0)

请查看此答案https://stackoverflow.com/a/36419269/3902739

它定义了Json对象数组的正确结构,并使用了JavaScript函数,例如Array.findArray.forEachArray.filter,如所提供的示例中清楚说明的那样。

希望这会有所帮助!

相关问题