admin管理员组文章数量:1327660
So I have objects that I want to be able to search for any matches of values.
For example:
let arr = [
{
a: 'foo',
b: 'bar'
},
{
a: 'bar',
b: 'baz'
},
{
a: 'foo',
b: 'baz'
}
];
and I want to be able to filter the array to contain any object that has a property with the value 'foo'.
Is there a way to do this with lodash? Something like:
_.filter(arr, function(obj) {
return obj.anyPropertiesMatch('bar');
});
So I have objects that I want to be able to search for any matches of values.
For example:
let arr = [
{
a: 'foo',
b: 'bar'
},
{
a: 'bar',
b: 'baz'
},
{
a: 'foo',
b: 'baz'
}
];
and I want to be able to filter the array to contain any object that has a property with the value 'foo'.
Is there a way to do this with lodash? Something like:
_.filter(arr, function(obj) {
return obj.anyPropertiesMatch('bar');
});
Share
Improve this question
asked Sep 18, 2017 at 18:16
DesquidDesquid
1312 silver badges12 bronze badges
1
- Possible duplicate of Filtering array of objects with lodash based on property value – jmargolisvt Commented Sep 18, 2017 at 18:19
4 Answers
Reset to default 4You can use Array.filter()
and check if the value exists, using Object.values()
to get an array of values, and Array.includes()
to check if they include val
:
const val = 'foo';
const arr = [{"a":"foo","b":"bar"},{"a":"bar","b":"baz"},{"a":"foo","b":"baz"}];
const result = arr.filter((o) => Object.values(o).includes(val));
console.log(result);
Or the lodash's equivalent using _.includes()
:
const val = 'foo';
const arr = [{"a":"foo","b":"bar"},{"a":"bar","b":"baz"},{"a":"foo","b":"baz"}];
const result = _.filter(arr, (o) => _.includes(o, val));
console.log(result);
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
It is as simple as this:
_.filter(arr, o=>_.includes(o,'foo'));
you can use lodash array function to any object directly and it will consider each values against each elements.
Here is an working example for you:
let arr = [
{
a: 'foo',
b: 'bar'
},
{
a: 'bar',
b: 'baz'
},
{
a: 'foo',
b: 'baz'
}
];
let res = _.filter(arr, o=>_.includes(o,'foo'));
console.log(res)
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.4/lodash.js"></script>
In lodash, you can use some
method to validate that object has the field with the provided value:
let arr = [
{ a: 'foo', b: 'bar' },
{ a: 'bar', b: 'baz' },
{ a: 'foo', b: 'baz' }
];
let result = _.filter(arr, obj => _.some(obj, val => val === 'foo'));
console.log(result);
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Using LoDash _.filter
to filter the array based on object values, gotten with _.values
and checked with _.includes
let arr = [
{
a: 'foo',
b: 'bar'
},
{
a: 'bar',
b: 'baz'
},
{
a: 'foo',
b: 'baz'
}
];
let res = _.filter(arr, o => _.includes(_.values(o), 'foo'));
console.log(res)
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.4/lodash.js"></script>
本文标签: javascriptHow to filter array on any matching value using lodashStack Overflow
版权声明:本文标题:javascript - How to filter array on any matching value using lodash? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742228483a2436772.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论