javascript Array.includes() 在数组中查找对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51603456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array.includes() to find object in array
提问by timothym
I'm attempting to find an object in an array using Array.prototype.includes. Is this possible? I realize there is a difference between shallow and deep comparison. Is that the reason the below code returns false? I could not find a relevant answer for Array.includes().
我正在尝试使用Array.prototype.includes. 这可能吗?我意识到浅比较和深比较是有区别的。这是下面代码返回false的原因吗?我找不到相关的答案Array.includes()。
回答by Mark Meyer
Array.includescompares by object identity just like obj === obj2, so sadly this doesn't work unless the two items are references to the same object. You can often use Array.prototype.some()instead which takes a function:
Array.includes像 一样按对象标识进行比较obj === obj2,因此很遗憾,除非这两个项目是对同一对象的引用,否则这不起作用。你可以经常使用它Array.prototype.some()来代替它接受一个函数:
const arr = [{a: 'b'}]
console.log(arr.some(item => item.a === 'b'))
But of course you then need to write a small function that defines what you mean by equality.
但是当然,您需要编写一个小函数来定义相等的含义。
回答by Ebi Igweze
You are on the right track but, the issue is the difference between reference and value types, you currently are using a reference type (object literal), so when you compare what is in the array with what you have, it will compare the references and not the values. this is what I mean:
您在正确的轨道上,但问题是引用类型和值类型之间的区别,您当前使用的是引用类型(对象文字),因此当您将数组中的内容与您拥有的内容进行比较时,它将比较引用而不是价值观。这就是我的意思:
var ar = [];
var x = {a: "b", c: "d" };
ar.push(x);
// this will log true because its the same reference
console.log("should be true: ", ar[0] === x);
ar.push({a: "b", c: "d" });
// this will log false because i have created
// a different reference for a new object.
console.log("should be false: ", ar[1] === x);
// Think of it like this
var obja = { foo: "bar" }; // new reference to 'obja'
var objb = { foo: "bar" }; // new reference to 'objb'
var valuea = 23;
var valueb = 23;
// 'obja' and 'obja' are different references
// although they contain same property & value
// so a test for equality will return false
console.log("should be false: ", obja === objb);
// on the other hand 'valuea' and 'valueb' are
// both value types, so an equality test will be true
console.log("should be true: ", valuea === valueb);
to achieve what you want, you either have to have added the actual reference, as I did above, or loop through the array and compare by unique property of the objects.
要实现您想要的,您要么必须添加实际引用,就像我上面所做的那样,要么遍历数组并按对象的唯一属性进行比较。
回答by Rahul Gandhi
Its' because both of the objects are not the same. Both are stored at different place in memory and the equality operation results false.
它的'因为两个对象都不相同。两者都存储在内存中的不同位置以及相等运算的结果false。
But if you search for the same object, then it will return true.
但是如果你搜索同一个对象,那么它会返回true.
Also, have a look at the below code, where you can understand that two identical objects also results falsewith the ===operator.
另外,看看下面的代码,您可以在其中理解两个相同的对象也false由===运算符产生。
For two objects to return truein ===, they should be pointing to same memory location.
对于返回truein 的两个对象===,它们应该指向相同的内存位置。
回答by A.J. Uppal
This is because the includeschecks to see if the object is in the array, which it in fact is not:
这是因为includes检查对象是否在数组中,而实际上它不在:
> {a: 'b'} === {a: 'b'}
false
This is because the test for equality is not testing if the objects are the same, but whether the point to the same object. Which they don't.
这是因为相等性的测试不是测试对象是否相同,而是测试是否指向同一个对象。他们没有。
回答by Bourja Hamid
you can use find to returns the value of the this element
您可以使用 find 返回此元素的值
const array = [{a: 'b'}];
array.includes(array.find(el=>el.a==='b'));
回答by Naofal Hakim
const arr = [{a: 'b',b:'c'},{a: 'c'}]
console.log(arr.some(item => item.b === 'c'))


