嵌套对象中多个值的对数过滤匹配 [英] Lodash filter matching for multiple values in nested objects

查看:40
本文介绍了嵌套对象中多个值的对数过滤匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象数组

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}

这篇关于嵌套对象中多个值的对数过滤匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆