[英]How to get the index of an object in an array of objects when it matches completely javascript
I have an array of objects and I would like to get the index of the object in the array when I get a match. 我有一个对象数组,当我获得匹配项时,我想获取对象在数组中的索引。
I have the array as follows: 我有如下数组:
let x = [
{name: "emily", info: { id: 123, gender: "female", age: 25}},
{name: "maggie", info: { id: 234, gender: "female", age: 22}},
{name: "kristy", info: { id: 564, gender: "female", age: 26}},
.....
];
Currently I am using indexOf
which worked initially and now it doesn't work properly. 目前,我正在使用
indexOf
,它最初可以工作,但现在不能正常工作。 It returns -1
. 返回
-1
。
let find = {name: "maggie", info: { id: 234, gender: "female", age: 22}};
let index = x.indexOf(find); // should return 1.
The whole should match in the array and should return the index of that object. 整体应该在数组中匹配,并且应该返回该对象的索引。 How can I achieve this?
我该如何实现? Should I be using
some()
? 我应该使用
some()
吗?
Thank you 谢谢
You can use .find
instead of indexOf
as 2 objects are never equal ( as they point to different reference in memory ) which is what you seem to pass as an argument. 您可以使用
.find
代替indexOf
因为2个对象永远都不相等(因为它们指向内存中的不同引用),这似乎是您作为参数传递的。
let x = [ {name: "emily", info: { id: 123, gender: "female", age: 25}}, {name: "maggie", info: { id: 234, gender: "female", age: 22}}, {name: "kristy", info: { id: 564, gender: "female", age: 26}} ]; let found = x.find(function(item) { // you can use the condition here return item.info.id === 564; }); console.log(found);
To find the index, you can use .findIndex
method instead. 要查找索引,可以改用
.findIndex
方法。
let x = [ {name: "emily", info: { id: 123, gender: "female", age: 25}}, {name: "maggie", info: { id: 234, gender: "female", age: 22}}, {name: "kristy", info: { id: 564, gender: "female", age: 26}} ]; let foundIndex = x.findIndex(function(item) { // you can use the condition here return item.info.id === 564; }); console.log(foundIndex);
Objects cannot be compared by traditional equality in JavaScript. 无法使用JavaScript中的传统相等性来比较对象。 Instead, use the ES6
findIndex
method to compare each object's properties with the desired values. 而是使用ES6
findIndex
方法将每个对象的属性与所需值进行比较。 Here is an example: 这是一个例子:
let x = [
{name: "emily", info: { id: 123, gender: "female", age: 25}},
{name: "maggie", info: { id: 234, gender: "female", age: 22}},
{name: "kristy", info: { id: 564, gender: "female", age: 26}}
];
let find = {name: "maggie", info: { id: 234, gender: "female", age: 22}};
let index = x.findIndex(element => element.info.id === find.info.id); // index === 1
The id
value seems to be sufficient to identify an object in your scenario; id
值似乎足以识别您的方案中的对象。 if you need to compare more properties, you could obviously add additional equality checks (eg, element.name === find.name
) with the &&
operator. 如果需要比较更多属性,则显然可以使用
&&
运算符添加其他相等性检查(例如element.name === find.name
)。
If we live in the _.lodash
world than this works since lodash would go deep on objects : 如果我们生活在
_.lodash
世界中, _.lodash
这行之有效,因为lodash将深入研究对象 :
let data = [ {name: "emily", info: { id: 123, gender: "female", age: 25}}, {name: "maggie", info: { id: 234, age: 22, gender: "female"}}, {name: "kristy", info: { id: 564, gender: "female", age: 26}}, ]; let find = {name: "maggie", info: { id: 234, gender: "female", age: 22}}; let index = _.findIndex(data, (i) => _.isEqual(i, find)) console.log(index) // 1
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
A more brutish approach which obviously it is not performant and as pointed out wont work if the order of the props is different. 一种更野蛮的方法,显然它不是高效的,并且指出,如果道具的顺序不同,这种方法将行不通。
let data = [ {name: "emily", info: { id: 123, gender: "female", age: 25}}, {name: "maggie", info: { id: 234, gender: "female", age: 22}}, {name: "kristy", info: { id: 564, gender: "female", age: 26}}, ]; var objectJSONs = data.map((i) => JSON.stringify(i)) let myJSON = JSON.stringify({name: "maggie", info: { id: 234, gender: "female", age: 22}}); let index = objectJSONs.indexOf(myJSON) console.log(index) // 1
You can make use of underscore _.isEqual for Object comparison and some() or any looping mechanism to iterate the array. 您可以使用下划线_.isEqual进行对象比较,并使用some()或任何循环机制来迭代数组。
let iFoundIndex = -1;
let bFound = x.some((data,index) => {
if(_.isEqual(data,find){
iFoundIndex = index;
return true;
}
return false;
}
//console.log(iFoundIndex);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.