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应返回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}
这篇关于嵌套对象中多个值的对数过滤匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:嵌套对象中多个值的对数过滤匹配
基础教程推荐
猜你喜欢
- HTML5 画布调整为父级 2022-01-01
- 即使每次插入第一个输入的值不同,第二个输入仍显示相同的输入值 2022-01-01
- 使用 jQuery 在悬停时交换 DIV 类 2022-01-01
- CORS:当凭据标志为真时,无法在 Access-Control-Allow-Origin 中使用通配符 2022-01-01
- 逻辑运算符 ||在 javascript 中,0 代表 Boolean false? 2022-01-01
- 带角度的选项卡:仅使用 $http 在单击时加载选项卡 2022-01-01
- 在 Javascript 中使用 Fetch API 上传文件并显示进度 2022-01-01
- 当木偶师打开Chrome时,不能使用Chrome扩展 2022-01-01
- 从快速中间件中排除路由 2022-01-01
- 最佳动态 JavaScript/JQuery 网格 2022-01-01
