[英]Using Array.prototype.includes to target property in an array of objects
With Array.prototype.includes
you can do something like this: 使用
Array.prototype.includes
您可以执行以下操作:
let array1 = [1, 2, 3];
console.log(array1.includes(2)); // return true
My question is: can you use includes
for an array of objects, where you want to find, say, "name" = "Jane"? 我的问题是:你可以使用
includes
一个对象数组,你想在哪里找到“name”=“Jane”? Take for instance the following data: 以下面的数据为例:
let array2 = [{"name" : "John", age: 24}, {"name" : "Jane", age: 36}]
Is this something you can do with the includes
method - and what would it look like? 这是你可以用
includes
方法做的事情 - 它会是什么样子?
You can use it, but only if you have the reference to the object ( not its structural equivalent). 您可以使用它,但前提是您有对象的引用( 而不是它的结构等价物)。
const o1 = { name: '1' }
const o2 = { name: '2' }
const arr = [o1, o2]
arr.includes(o1) // true
arr.includes({ name: '1' }) // false
This is because includes
uses the "SameValueZero" algorithm, as per the spec (emphasis mine): 这是因为
includes
使用“SameValueZero”算法, 根据规范 (强调我的):
includes
comparessearchElement
to the elements of the array, in ascending order, using the SameValueZero algorithm , and if found at any position, returnstrue
;includes
使用SameValueZero算法按searchElement
将searchElement
与数组元素进行比较,如果在任何位置找到,则返回true
; otherwise,false
is returned.否则,返回
false
。
" SameValueZero " will always return false for different references, hence the second attempt from the code above will return false
. 对于不同的引用,“ SameValueZero ”将始终返回false,因此上述代码的第二次尝试将返回
false
。
You can use Array#some
instead, which lets you specify a lambda as the parameter. 您可以使用
Array#some
,它允许您指定lambda作为参数。 Then you can write your custom equality logic even for objects. 然后,您甚至可以为对象编写自定义相等逻辑。
arr.some(o => o.name == '1') // true
From your comment , I see you're interested in checking against multiple values. 从您的评论中 ,我发现您对检查多个值感兴趣。 You can simply use the
||
你可以简单地使用
||
operator with some
in this case: 在这种情况下有
some
运算符:
arr.some(o => o.name == '1' || o.name == '2' || o.name == '3')
If you do not wish to specify all of them like this, you can do something like the following. 如果您不希望像这样指定所有这些,您可以执行以下操作。
arr.some(o => ['1', '2', '3'].includes(o.name))
In that case you could use some
method instead and it will return true
on first match. 在这种情况下,您可以使用
some
方法,它将在第一次匹配时返回true
。
let array = [{"name" : "John", age: 24}, {"name" : "Jane", age: 36}] let check = array.some(({name}) => name == 'Jane'); console.log(check)
array.find(user => user.name === "Jane")
As objects get compared by reference, .includes
doesn't help you in most cases, then you have to through all elements manually and check for equality, thats what .find
does. 当对象通过引用进行比较时,
.includes
在大多数情况下对你没有帮助,那么你必须手动通过所有元素并检查是否相等,这就是.find
所做的。 It either returns the found user or undefined
. 它要么返回找到的用户,要么返回
undefined
。 If you just need a boolean use .some
that works like .find
but either returns true or false. 如果你只需要一个布尔使用
.some
就像.find
一样,但要么返回true或false。
You can do this. 你可以这样做。
let array2 = [{ "name": "John", age: 24 }, { "name": "Jane", age: 36 }] var some = array2.some((i) => { return i.name === 'Jane' }) var map = array2.map((i) => { return i.name === 'Jane' }) console.log('some', some) console.log('map', map)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.