0

i'm trying to filter a deep "complex" nested object array! Trying to figured out with _.filterDeep().

Initial data :

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
                                    }
                                ]
                            },
                        ]
                    },
                ],
            },
        ],
    },

...

]

Expected output :

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
                                    },
                                ]
                            },
                        ]
                    }
                ],
            },
        ],
    }
]

Current output:

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', 
                                    },
                                ]
                            },
                        ]
                    }
                ],
            },
        ],
    }
]

Actual function :

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);
}

Technically the function works but the mistake is that the hierarchy and the other object key are removed from the array.

What i try is to keep parent hierarchy if any object still in the child array.

Try and search a lot of different solutions like { childrenPath:'products_list' } -- option to declare children property name, to make deepdash work in 'tree mode'

Any help is very much appreciated. :)

Bueno
  • 11
  • 2

1 Answers1

1

Okey, found a solution :)

I simplify the children array key name based on this best answer : Recursively filter array of objects

New array structure :

const products = [
    { 
        name: 'Food to Go',
        filter: 'food',
        color: '#f9dd0a',
        children: [
            { 
                name: 'Bepulp Compostable',
                children: [
                    {
                        name: 'BOWLS & CONTAINERS',
                        children: [
                            {
                                name: 'RECTANGULAR',
                                children: [
                                    {
                                        name: 'PUL46120', 
                                        color: 'natural', 
                                        code: 'PUL46120', 
                                        description: 'Rectangular tray 600ml 16x23 cm', 
                                        capacity: '600ml', 
                                        dimensions: '16x23x3',
                                        packaging: '4x75p.',
                                        cases: '25',
                                        material: 'PULP',
                                        cc: 'false',
                                        hot: 'false'
                                    },
                                    ....

And the function :

function copy(o) {
  return Object.assign({}, o)
}

products.map(copy).filter(function f(o) {
      if (o.name && o.name.toLowerCase().indexOf(searchVal) != -1) return true
      if (o.description && o.description.toLowerCase().indexOf(searchVal) != -1) return true
      ...

      if (o.children) {
        return (o.children = o.children.map(copy).filter(f)).length
      }
})

Maybe can help someone ;)

Bueno
  • 11
  • 2