嵌套对象中多个值的对数过滤匹配

2022-03-09 00:00:00 reactjs javascript lodash

我有一个对象数组

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"
}

匹配后的预期结果

  • 匹配objselectedAttributes应返回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}

相关文章