Javascript 当对象键值在数组中时如何过滤数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35817565/
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
How to filter array when object key value is in array
提问by krsnaadi
I have an array model as below:
我有一个数组模型如下:
records:[{
"empid":1,
"fname": "X",
"lname": "Y"
},
{
"empid":2,
"fname": "A",
"lname": "Y"
},
{
"empid":3,
"fname": "B",
"lname": "Y"
},
{
"empid":4,
"fname": "C",
"lname": "Y"
},
{
"empid":5,
"fname": "C",
"lname": "Y"
}
]
Now I have an array of empid's [1,4,5]
.
现在我有一组 empid 的[1,4,5]
.
So now I need to filter the first array which contains all the keys in my second.
所以现在我需要过滤包含第二个中所有键的第一个数组。
Output:
输出:
records:[{
"empid":1,
"fname": "X",
"lname": "Y"
},
{
"empid":4,
"fname": "C",
"lname": "Y"
},
{
"empid":5,
"fname": "C",
"lname": "Y"
}
]
I can do this using a forEach
loop in angular
but as I have more than 100 records in my model object. I need a suggestion on how to handle this in much better way.
我可以使用forEach
循环来做到这一点,angular
但因为我的模型对象中有 100 多条记录。我需要一个关于如何以更好的方式处理这个问题的建议。
I am thinking of creating a custom filter, but what is your take on it.(If yes please provide sample code to achieve this).
我正在考虑创建一个自定义过滤器,但您对此有何看法。(如果是,请提供示例代码来实现这一点)。
Your Help Is Appreciated.
感谢您的帮助。
Thanks.
谢谢。
回答by Rajaprabhu Aravindasamy
You can do it with Array.prototype.filter()
,
你可以这样做Array.prototype.filter()
,
var data = { records : [{ "empid": 1, "fname": "X", "lname": "Y" }, { "empid": 2, "fname": "A", "lname": "Y" }, { "empid": 3, "fname": "B", "lname": "Y" }, { "empid": 4, "fname": "C", "lname": "Y" }, { "empid": 5, "fname": "C", "lname": "Y" }] }
var empIds = [1,4,5]
var filteredArray = data.records.filter(function(itm){
return empIds.indexOf(itm.empid) > -1;
});
filteredArray = { records : filteredArray };
If? the ?callBack
? returns a ?true
? value, then the ?itm
? passed to that particular callBack
will be filtered out. You can read more about it here.??????
如果?这 ?callBack
? 返回一个 ? true
? 值,那么 ? itm
? 传递给那个特定的callBack
将被过滤掉。你可以在这里阅读更多关于它的信息。????
回答by Tim Elsass
In 2019 using ES6:
2019 年使用 ES6:
const ids = [1, 4, 5],
data = {
records: [{
"empid": 1,
"fname": "X",
"lname": "Y"
}, {
"empid": 2,
"fname": "A",
"lname": "Y"
}, {
"empid": 3,
"fname": "B",
"lname": "Y"
}, {
"empid": 4,
"fname": "C",
"lname": "Y"
}, {
"empid": 5,
"fname": "C",
"lname": "Y"
}]
};
data.records = data.records.filter( i => ids.includes( i.empid ) );
console.info( data );
回答by Nina Scholz
This is a fast solution with a temporary object.
这是使用临时对象的快速解决方案。
var records = [{ "empid": 1, "fname": "X", "lname": "Y" }, { "empid": 2, "fname": "A", "lname": "Y" }, { "empid": 3, "fname": "B", "lname": "Y" }, { "empid": 4, "fname": "C", "lname": "Y" }, { "empid": 5, "fname": "C", "lname": "Y" }],
empid = [1, 4, 5],
object = {},
result;
records.forEach(function (a) {
object[a.empid] = a;
});
result = empid.map(function (a) {
return object[a];
});
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
回答by isvforall
You can use Array#filter
function and additional array for storing sorted values;
您可以使用Array#filter
函数和附加数组来存储排序值;
var recordsSorted = []
ids.forEach(function(e) {
recordsSorted.push(records.filter(function(o) {
return o.empid === e;
}));
});
console.log(recordsSorted);
Result:
结果:
[ [ { empid: 1, fname: 'X', lname: 'Y' } ],
[ { empid: 4, fname: 'C', lname: 'Y' } ],
[ { empid: 5, fname: 'C', lname: 'Y' } ] ]
回答by Ashutosh Tripathy
Fastest way (will take extra memory):
最快的方式(需要额外的内存):
var empid=[1,4,5]
var records = [{ "empid": 1, "fname": "X", "lname": "Y" }, { "empid": 2, "fname": "A", "lname": "Y" }, { "empid": 3, "fname": "B", "lname": "Y" }, { "empid": 4, "fname": "C", "lname": "Y" }, { "empid": 5, "fname": "C", "lname": "Y" }] ;
var empIdObj={};
empid.forEach(function(element) {
empIdObj[element]=true;
});
var filteredArray=[];
records.forEach(function(element) {
if(empIdObj[element.empid])
filteredArray.push(element)
});
回答by Peter van der Lely
In case you have key value pairs in your input array, I used:
如果您的输入数组中有键值对,我使用了:
.filter(
this.multi_items[0] != null && store.state.isSearchBox === false
? item =>
_.map(this.multi_items, "value").includes(item["wijknaam"])
: item => item["wijknaam"].includes("")
);
where the input array is multi_items as: [{"text": "bla1", "value": "green"}, {"text": etc. etc.}]
其中输入数组是 multi_items 为:[{"text": "bla1", "value": "green"}, {"text": etc. etc.}]
_.map is a lodash function.
_.map 是一个 lodash 函数。