This question already has an answer here:
这个问题在这里已有答案:
- Javascript: How to filter object array based on attributes? 10 answers
- Javascript:如何根据属性过滤对象数组? 10个答案
I am trying to get an object in an array by the value of one of its keys.
我试图通过其中一个键的值获取数组中的对象。
The array:
数组:
var arr = [
{
city: 'Amsterdam',
title: 'This is Amsterdam!'
},
{
city: 'Berlin',
title: 'This is Berlin!'
},
{
city: 'Budapest',
title: 'This is Budapest!'
}
];
I tried doing something like this with lodash
but no success.
我尝试用lodash做这样的事情,但没有成功。
var picked = lodash.pickBy(arr, lodash.isEqual('Amsterdam');
and it returns an empty object.
它返回一个空对象。
Any idea on how I can do this the lodash way (if it's even possible)? I can do it the classic way, creating a new array, looping through all objects and pushing the ones matching my criteria to that new array. But is there a way to do it with lodash?
关于如何做到这一点的任何想法lodash方式(如果它甚至可能)?我可以用经典的方式做到这一点,创建一个新的数组,循环遍历所有对象,并将符合我标准的对象推送到新数组。但有没有办法用lodash做到这一点?
This is NOT a duplicate.
这不是重复的。
4 个解决方案
#1
9
Using lodash and an arrow function, it should be as simple as;
使用lodash和箭头功能,应该如此简单;
var picked = lodash.filter(arr, x => x.city === 'Amsterdam');
...or alternately with object notation;
......或者用对象符号交替;
var picked = lodash.filter(arr, { 'city': 'Amsterdam' } );
Note: The above answer used to be based on pickBy
, which as @torazaburo points out below was not a good choice for the use case.
注意:上面的答案过去基于pickBy,正如@torazaburo在下面指出的那样,对于用例来说不是一个好的选择。
#2
30
You can use Array.prototype.find() with pure javascript:
您可以将Array.prototype.find()与纯javascript一起使用:
var picked = arr.find(o => o.city === 'Amsterdam');
It is currently not compatible with all browsers, you need to check it in your environment (but it should work in NodeJS).
它目前与所有浏览器不兼容,您需要在您的环境中检查它(但它应该在NodeJS中工作)。
#3
2
classical way is even simpler
经典的方式更简单
try
尝试
var output = arr.filter(function(value){ return value.city=="Amsterdam";})
#4
1
You can use Array.filter
您可以使用Array.filter
As correctly pointed by @torazaburo, you do not need ternary operator return item[key]?item[key] === value:false
. A simple check return item[key] === value
will do fine.
正如@torazaburo正确指出的那样,你不需要三元运算符返回项[key]?item [key] === value:false。一个简单的检查返回项[键] ===值就可以了。
var arr = [{
city: 'Amsterdam',
title: 'This is Amsterdam!'
}, {
city: 'Berlin',
title: 'This is Berlin!'
}, {
city: 'Budapest',
title: 'This is Budapest!'
}];
Array.prototype.findByValueOfObject = function(key, value) {
return this.filter(function(item) {
return (item[key] === value);
});
}
document.write("<pre>" + JSON.stringify(arr.findByValueOfObject("city", "Amsterdam"), 0, 4) + "</pre>");
#1
9
Using lodash and an arrow function, it should be as simple as;
使用lodash和箭头功能,应该如此简单;
var picked = lodash.filter(arr, x => x.city === 'Amsterdam');
...or alternately with object notation;
......或者用对象符号交替;
var picked = lodash.filter(arr, { 'city': 'Amsterdam' } );
Note: The above answer used to be based on pickBy
, which as @torazaburo points out below was not a good choice for the use case.
注意:上面的答案过去基于pickBy,正如@torazaburo在下面指出的那样,对于用例来说不是一个好的选择。
#2
30
You can use Array.prototype.find() with pure javascript:
您可以将Array.prototype.find()与纯javascript一起使用:
var picked = arr.find(o => o.city === 'Amsterdam');
It is currently not compatible with all browsers, you need to check it in your environment (but it should work in NodeJS).
它目前与所有浏览器不兼容,您需要在您的环境中检查它(但它应该在NodeJS中工作)。
#3
2
classical way is even simpler
经典的方式更简单
try
尝试
var output = arr.filter(function(value){ return value.city=="Amsterdam";})
#4
1
You can use Array.filter
您可以使用Array.filter
As correctly pointed by @torazaburo, you do not need ternary operator return item[key]?item[key] === value:false
. A simple check return item[key] === value
will do fine.
正如@torazaburo正确指出的那样,你不需要三元运算符返回项[key]?item [key] === value:false。一个简单的检查返回项[键] ===值就可以了。
var arr = [{
city: 'Amsterdam',
title: 'This is Amsterdam!'
}, {
city: 'Berlin',
title: 'This is Berlin!'
}, {
city: 'Budapest',
title: 'This is Budapest!'
}];
Array.prototype.findByValueOfObject = function(key, value) {
return this.filter(function(item) {
return (item[key] === value);
});
}
document.write("<pre>" + JSON.stringify(arr.findByValueOfObject("city", "Amsterdam"), 0, 4) + "</pre>");