Lodash 过滤器匹配嵌套对象中的多个值
Lodash filter matching for multiple values in nested objects
我有一个对象数组
const obj = [
{
"id": 20000,
"attributes": [
{
"name": "Size",
"value": "38"
},
{
"name": "Color",
"value": "Blue"
}
]
},
{
"id": 20056,
"attributes": [
{
"name": "Size",
"value": "38"
},
{
"name": "Color",
"value": "Brown"
}
]
}
]
我还有另一个对象要与
匹配
const selectedAttributes = {
"Color": "Brown",
"Size": "38"
}
匹配后想要的结果
- 将
obj
与 selectedAttributes
匹配应该 return obj 中的 second entry
我正在尝试什么
由于我们不能在 ._matches 中提供多个值 属性 这不起作用
matchingVariations = _.filter(obj, _.flow(
_.property('attributes'),
_.partialRight(_.some, { name: ['Size', 'Color'], value: ['Brown', '38'] })
))
预期输出
[
{
"id": 20056,
"attributes": [
{
"name": "Size",
"value": "38"
},
{
"name": "Color",
"value": "Brown"
}
]
}
]
const obj = [
{
"id": 20000,
"attributes": [
{
"name": "Size",
"value": "38"
},
{
"name": "Color",
"value": "Blue"
}
]
},
{
"id": 20056,
"attributes": [
{
"name": "Size",
"value": "38"
},
{
"name": "Color",
"value": "Brown"
}
]
}
]
const selectedAttributes = {
"Color": "Brown",
"Size": "38"
}
const selectedAttributes2 = {
"Size": "38"
}
const matchingVariations = _.filter(obj, _.flow(
_.property('attributes'),
_.partialRight(_.some, { name: 'Size', value: '38' })
))
const matchingVariations2 = _.filter(obj, _.flow(
_.property('attributes'),
_.partialRight(_.some, { name: ['Size', 'Color'], value: ['Brown', '38'] })
))
console.log(matchingVariations);
console.log(matchingVariations2);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
没有库,你可以直接使用Array#filter
只获取特定元素。
Object.entries
可用于获取所选属性的键值对,Array#every
可用于确保所有条目匹配给定条件,Array#some
可用于检查对象的属性数组中是否存在键和值。
const obj=[{id:2000,attributes:[{name:"Size",value:"38"},{name:"Color",value:"Blue"}]},{id:20056,attributes:[{name:"Size",value:"38"},{name:"Color",value:"Brown"}]}];
const selectedAttributes = {
"Color": "Brown",
"Size": "38"
}
const selectedAttributes2 = {
"Size": "38"
}
const matches = (arr, attrs)=>arr.filter(x =>
Object.entries(attrs).every(([k, v])=>
x.attributes.some(a => a.name === k && a.value === v
)));
console.log(matches(obj, selectedAttributes));
console.log(matches(obj, selectedAttributes2));
.as-console-wrapper{max-height:100%!important;top:0}
我有一个对象数组
const obj = [
{
"id": 20000,
"attributes": [
{
"name": "Size",
"value": "38"
},
{
"name": "Color",
"value": "Blue"
}
]
},
{
"id": 20056,
"attributes": [
{
"name": "Size",
"value": "38"
},
{
"name": "Color",
"value": "Brown"
}
]
}
]
我还有另一个对象要与
匹配const selectedAttributes = {
"Color": "Brown",
"Size": "38"
}
匹配后想要的结果
- 将
obj
与selectedAttributes
匹配应该 return obj 中的
second entry
我正在尝试什么
由于我们不能在 ._matches 中提供多个值 属性 这不起作用
matchingVariations = _.filter(obj, _.flow(
_.property('attributes'),
_.partialRight(_.some, { name: ['Size', 'Color'], value: ['Brown', '38'] })
))
预期输出
[
{
"id": 20056,
"attributes": [
{
"name": "Size",
"value": "38"
},
{
"name": "Color",
"value": "Brown"
}
]
}
]
const obj = [
{
"id": 20000,
"attributes": [
{
"name": "Size",
"value": "38"
},
{
"name": "Color",
"value": "Blue"
}
]
},
{
"id": 20056,
"attributes": [
{
"name": "Size",
"value": "38"
},
{
"name": "Color",
"value": "Brown"
}
]
}
]
const selectedAttributes = {
"Color": "Brown",
"Size": "38"
}
const selectedAttributes2 = {
"Size": "38"
}
const matchingVariations = _.filter(obj, _.flow(
_.property('attributes'),
_.partialRight(_.some, { name: 'Size', value: '38' })
))
const matchingVariations2 = _.filter(obj, _.flow(
_.property('attributes'),
_.partialRight(_.some, { name: ['Size', 'Color'], value: ['Brown', '38'] })
))
console.log(matchingVariations);
console.log(matchingVariations2);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
没有库,你可以直接使用Array#filter
只获取特定元素。
Object.entries
可用于获取所选属性的键值对,Array#every
可用于确保所有条目匹配给定条件,Array#some
可用于检查对象的属性数组中是否存在键和值。
const obj=[{id:2000,attributes:[{name:"Size",value:"38"},{name:"Color",value:"Blue"}]},{id:20056,attributes:[{name:"Size",value:"38"},{name:"Color",value:"Brown"}]}];
const selectedAttributes = {
"Color": "Brown",
"Size": "38"
}
const selectedAttributes2 = {
"Size": "38"
}
const matches = (arr, attrs)=>arr.filter(x =>
Object.entries(attrs).every(([k, v])=>
x.attributes.some(a => a.name === k && a.value === v
)));
console.log(matches(obj, selectedAttributes));
console.log(matches(obj, selectedAttributes2));
.as-console-wrapper{max-height:100%!important;top:0}