简体   繁体   English

完全匹配javascript时如何获取对象数组中的对象索引

[英]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.

相关问题 如何通过索引从对象数组中的对象数组中删除对象javascript - How to delete object from array of objects in array of objects javascript by index 如何获得对象数组中对象的索引并进行拼接? - How to get index of object in array of objects and splice it? 如何在对象数组中获取对象的索引? - How to get the index of an object inside an array of objects? 如果 object 和数组值在 javascript 中匹配,如何获取 object 密钥 - how to get the object key if the object and array value matches in javascript 对象数组中javascript对象的索引 - Index of javascript object in array of objects 获取特定ID上的对象数组中的对象索引-javascript或jQuery - Get index of object in array of objects on a particular id - javascript or jQuery javascript:如何获取关联数组中对象的索引? - javascript: how to get index of an object in an associative array? 如何通过 88222995402888 和 javascript 中的 arrays 获取对象数组 - How to get array of objects by object and arrays in javascript 如何在对象数组的嵌套数组中获取数组对象javascript - How to get array object in nested array of array of objects javascript 从对象数组中,如果对象的 valueA 与变量匹配,如何获取对象的 valueB? - From an array of objects, How to get an object's valueB if that object's valueA matches a variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM