javascript 查找对象数组中的所有匹配元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52311150/
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
Find all matching elements with in an array of objects
提问by ankur
I have an array of objects
我有一个对象数组
I am searching within the array like this
我在这样的数组中搜索
let arr = [
{ name:"string 1", arrayWithvalue:"1,2", other: "that" },
{ name:"string 2", arrayWithvalue:"2", other: "that" },
{ name:"string 2", arrayWithvalue:"2,3", other: "that" },
{ name:"string 2", arrayWithvalue:"4,5", other: "that" },
{ name:"string 2", arrayWithvalue:"4", other: "that" },
];
var item = arr.find(item => item.arrayWithvalue === '4');
console.log(item)
This should return an array with this two rows
这应该返回一个包含这两行的数组
{ name:"string 2", arrayWithvalue:"4,5", other: "that" },
{ name:"string 2", arrayWithvalue:"4", other: "that" }
It returns only one row which is the first match.
它只返回第一行匹配项。
{ name:"string 2", arrayWithvalue:"4", other: "that" }
I do not want to use any external libraries for this. How can I return all the matches that match the criteria?
我不想为此使用任何外部库。如何返回符合条件的所有匹配项?
回答by Grégory NEUT
Two thing, first, Array.find()return the first matching element, undefinedif it finds nothing. Array.filterreturns a new array containing all matching elements, []if it matches nothing.
两件事,首先,Array.find()返回第一个匹配元素,undefined如果它什么也没找到。如果不匹配Array.filter,[]则返回一个包含所有匹配元素的新数组。
Second thing, if you want to match 4,5, you have to look into the string and do not make strict comparaison. To make that happens we use of indexOfwhich is returning the position of the matching string, or -1if it matches nothing.
其次,如果你想匹配4,5,你必须查看字符串,不要进行严格的比较。为了实现这一点,我们使用indexOfwhich 返回匹配字符串的位置,或者-1如果它不匹配。
Example:
示例:
const arr = [{
name: 'string 1',
arrayWithvalue: '1,2',
other: 'that',
},
{
name: 'string 2',
arrayWithvalue: '2',
other: 'that',
},
{
name: 'string 2',
arrayWithvalue: '2,3',
other: 'that',
},
{
name: 'string 2',
arrayWithvalue: '4,5',
other: 'that',
},
{
name: 'string 2',
arrayWithvalue: '4',
other: 'that',
},
];
const items = arr.filter(item => item.arrayWithvalue.indexOf('4') !== -1);
console.log(items);
回答by Adam
Array.prototype.find()will, as per the MDN spec: return the value of the first element in the array that satisfies the provided testing function.
Array.prototype.find()将根据MDN 规范:返回数组中满足提供的测试函数的第一个元素的值。
What you want to use instead is the filter function.filter()which will return an array of all instances that match your testing function.
您要使用的是filter 函数.filter(),它将返回与您的测试函数匹配的所有实例的数组。
回答by Richard Millen
You need to use the filtermethod in place of find. This will return a new array containing just the members that return a truthy value from the passed in function.
您需要使用该filter方法来代替find。这将返回一个新数组,该数组仅包含从传入函数返回真值的成员。
回答by Eklavya
Use array filter method. Like
使用数组过滤方法。喜欢
arr.filter(res => res.arrayWithvalue.indexOf('4') !== -1);
回答by Faly
Use array.filter:
使用 array.filter:
var arr = [
{ name:"string 1", arrayWithvalue:"1,2", other: "that" },
{ name:"string 2", arrayWithvalue:"2", other: "that" },
{ name:"string 2", arrayWithvalue:"2,3", other: "that" },
{ name:"string 2", arrayWithvalue:"4,5", other: "that" },
{ name:"string 2", arrayWithvalue:"4", other: "that" },
];
var res = arr.filter(e => e.arrayWithvalue.split(',')[0] === '4');
console.log(res);

