使用属性名称在javascript中查找对象的索引 [英] Find index of object in javascript using its property name
本文介绍了使用属性名称在javascript中查找对象的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用对象属性名称查找对象的javascript数组的索引.我的代码是:-
I want to find Index of javascript array of objects using objects property name. My code is :-
const checkbox = [{'mumbai': true},{'bangalore': true},{'chennai': true},{'kolkata': true}];
我如何找到钦奈的索引?我可以使用lodash来达到目的吗?
How can i find index of chennai? Can i acheive using lodash?
推荐答案
You can use .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中查找对象的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文