我正在尝试过滤一个深度“复杂”嵌套对象数组!尝试使用_.filterDeep()进行计算。
初始数据:
const products_array = [
{
name: 'Food to Go',
filter: 'food',
color: '#f9dd0a',
categories_list: [
{
name: 'Bepulp Compostable',
sub_categories: [
{
name: 'BOWLS & CONTAINERS',
products: [
{
type: 'RECTANGULAR',
products_list: [
{
color: 'natural',
code: 'PAP46120',
description: 'Rectangular tray 600ml 16x23 cm',
capacity: '600ml',
dimensions: '16x23x3',
packaging: '4x75p.',
cases: '25',
material: 'PULP',
cc: false,
hot: false
},
{
color: 'natural',
code: 'PUL46130',
description: 'Rectangular tray 950ml 16x23 cm',
capacity: '950ml',
dimensions: '16x23x3',
packaging: '4x75p.',
cases: '25',
material: 'PULP',
cc: false,
hot: false
},
{
color: 'clear',
code: 'PUL51601',
description: 'rPET lid for rectangular tray 16x23 cm',
capacity: '',
dimensions: '16x23x2',
packaging: '4x75p.',
cases: '20',
material: 'rPET',
cc: false,
hot: false
},
]
},
{
type: 'SQUARE',
products_list: [
{
color: 'natural',
code: 'PUL15012',
description: 'Square bowl 375ml 13x13 cm',
capacity: '375ml',
dimensions: '13x13x5',
packaging: '10x50p.',
cases: '25',
material: 'PULP',
cc: false,
hot: false
},
{
color: 'natural',
code: 'PUL15016',
description: 'Square bowl 500ml 13x13 cm',
capacity: '500ml',
dimensions: '13x13x6',
packaging: '10x50p.',
cases: '25',
material: 'PULP',
cc: false,
hot: false
}
]
}
]
},
{
name: 'GRAB & GO',
products: [
{
type: '',
products_list: [
{
color: 'natural',
code: 'PUL400606',
description: 'GRAB & GO square tray 13x13 cm',
capacity: '',
dimensions: '13x13x3',
packaging: '5x50p.',
cases: '88',
material: 'PULP',
cc: false,
hot: false
},
]
}
]
},
{
name: 'BOXES TO GO',
products: [
{
type: 'Hamburger box',
products_list: [
{
color: 'white',
code: 'PUL2014N',
description: 'Hamburger box 800ml 15x15 cm',
capacity: '800ml',
dimensions: '15x15x8',
packaging: '12x50p.',
cases: '13',
material: 'PULP',
cc: true,
hot: false
},
]
},
]
}
],
},
{
name: 'Recyclable Paper',
sub_categories: [
{
name: '',
products: [
{
type: 'PAPER CUTLERY',
products_list: [
{
color: 'white',
code: 'PAP3510',
description: 'Fork',
capacity: '',
dimensions: '17',
packaging: '20x50p.',
cases: '70',
material: 'Paper',
cc: true,
hot: false,
isNew: true
},
]
},
{
type: 'SNAP & GO',
products_list: [
{
color: 'Kraft',
code: 'PAP15KSG375',
description: 'Salad tray 12oz',
capacity: '375',
dimensions: '15x12x5',
packaging: '1x400',
cases: '',
material: 'Paper',
cc: false,
hot: false,
isNew: true
},
]
},
]
},
],
},
],
},
{
name: 'BEVERAGE SOLUTIONS',
filter: 'beverage',
color: '#0ad5f9',
categories_list: [
{
name: '',
sub_categories: [
{
name: '',
products: [
{
type: 'Beverage on the Move',
products_list: [
{
color: 'Kraft',
code: 'PAPBOTM2417',
description: 'Bag-In-Box KRAFT 2,8L / 96 oz',
capacity: '2800ml',
dimensions: '24x17x16',
packaging: '10p',
cases: '36',
material: 'Paper/other',
cc: false,
hot: false
}
]
},
]
},
],
},
],
},
...
]
预期输出:
const products_array = [
{
name: 'Food to Go',
filter: 'food',
color: '#f9dd0a',
categories_list: [
{
name: 'Bepulp Compostable',
sub_categories: [
{
name: 'BOWLS & CONTAINERS',
products: [
{
type: 'RECTANGULAR',
products_list: [
{
color: 'natural',
code: 'PUL46130',
description: 'Rectangular tray 950ml 16x23 cm',
capacity: '950ml',
dimensions: '16x23x3',
packaging: '4x75p.',
cases: '25',
material: 'PULP',
cc: false,
hot: false
},
{
color: 'clear',
code: 'PUL51601',
description: 'rPET lid for rectangular tray 16x23 cm',
capacity: '',
dimensions: '16x23x2',
packaging: '4x75p.',
cases: '20',
material: 'rPET',
cc: false,
hot: false
},
]
},
{
type: 'SQUARE',
products_list: [
{
color: 'natural',
code: 'PUL15012',
description: 'Square bowl 375ml 13x13 cm',
capacity: '375ml',
dimensions: '13x13x5',
packaging: '10x50p.',
cases: '25',
material: 'PULP',
cc: false,
hot: false
},
{
color: 'natural',
code: 'PUL15016',
description: 'Square bowl 500ml 13x13 cm',
capacity: '500ml',
dimensions: '13x13x6',
packaging: '10x50p.',
cases: '25',
material: 'PULP',
cc: false,
hot: false
}
]
}
]
},
{
name: 'GRAB & GO',
products: [
{
type: '',
products_list: [
{
color: 'natural',
code: 'PUL400606',
description: 'GRAB & GO square tray 13x13 cm',
capacity: '',
dimensions: '13x13x3',
packaging: '5x50p.',
cases: '88',
material: 'PULP',
cc: false,
hot: false
},
]
}
]
},
{
name: 'BOXES TO GO',
products: [
{
type: 'Hamburger box',
products_list: [
{
color: 'white',
code: 'PUL2014N',
description: 'Hamburger box 800ml 15x15 cm',
capacity: '800ml',
dimensions: '15x15x8',
packaging: '12x50p.',
cases: '13',
material: 'PULP',
cc: true,
hot: false
},
]
},
]
}
],
},
],
}
]
电流输出:
const products_array = [
{
categories_list: [
{
sub_categories: [
{
products: [
{
products_list: [
{
code: 'PUL46130',
},
{
code: 'PUL51601',
},
]
},
{
products_list: [
{
code: 'PUL15012',
},
{
code: 'PUL15016',
}
]
}
]
},
{
products: [
{
products_list: [
{
code: 'PUL400606',
},
]
}
]
},
{
products: [
{
products_list: [
{
code: 'PUL2014N',
},
]
},
]
}
],
},
],
}
]
实际功能:
function arrayFilter(products_array, searchVal = 'PUL') {
let list = _.filterDeep(
products_array,
function(value, key) {
if (key == 'code') {
return value.indexOf(searchVal) >= 0;
}
},
{
onTrue: { skipChildren: false },
}
);
console.log(list);
}
从技术上讲,该函数可以工作,但错误是层次结构和其他对象键从数组中删除。
我试图保持父层次结构,如果任何对象仍然在子数组中。
尝试搜索很多不同的解决方案,如{ childrenPath:'products_list' } -- option
来声明子属性名,使deepdash在“树模式”下工作
任何帮助都非常感谢。:)
1条答案
按热度按时间cetgtptt1#
OK,找到解决方案了:)
我根据这个最佳答案简化了子数组的键名:Recursively filter array of objects
新数组结构:
和函数:
也许可以帮助别人;)