javascript - 如何使用Javascript在数组中查找json对象的索引
问题描述
我有一个如下所示的数组
let formulaArray = ["(", "(", "(", "2", "+", "3", ")", "*", "3", ")", "+", "(", "12", "/",{}]
}
数组中的 json 对象可以在任何索引处。现在,我想找到 json 对象的索引,我该怎么做?
解决方案
使用.forEach()
然后用typeof
运算符检查对象,如果为真,则将其推送到数组
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)
推荐阅读
- reactjs - 生产服务器中 reactjs Web 应用程序的最佳方式是什么
- spring-boot - Spring Boot 中 Exchange (Camel) 的等价对象是什么?
- sonos - 移动设备上 Spotify 音乐服务中使用的未知(未记录)SONOS 显示类型
- react-native - React-Native 视频不播放音频
- oracle - 选择子句中的 Oracle `partition_by`,它是否永久创建这些分区?
- javascript - 如果动态添加元素,如何防止列在网格中增长?
- google-apps-script - 我可以自动将标题文本添加到 Google 文档文件的页脚吗?
- html - 带有列渐变的样式 html 表格
- php - Codeigniter 4:从控制器运行 phpUnit
- sql - 可以使用什么运算符代替在条件之间具有 AND 的 IN?