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

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

匹配后的预期结果

  • 匹配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}

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)