我想使用对象属性名称查找对象的 javascript 数组的索引。我的代码是:-
const checkbox = [{'mumbai': true},{'bangalore': true},{'chennai': true},{'kolkata': true}];
如何找到 chennai 的索引?我可以使用 lodash 实现吗?
最佳答案
您可以使用.findIndex()
const checkbox = [
{'mumbai': true},
{'bangalore': true},
{'chennai': true},
{'kolkata': true}
];
const finder = (arr, key) => arr.findIndex(o => key in o);
console.log(finder(checkbox, 'chennai'));
console.log(finder(checkbox, 'kolkata'));
console.log(finder(checkbox, 'delhi'));
关于javascript - 使用属性名称在 javascript 中查找对象的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54665358/