I want to collect all the particular object from nested object array object

Viewed 44

I want to collect all the object of a particular category from the nested array-object data in to one single array

let categoriesxy= [
        {
            catid: 'category-1',
            product: [
                {
                    id : 1,
                    name: 'sparrow'
                },
                {
                    id : 2,
                    name: 'parrot',
                }
            ]

        },
        {
            catid: 'category-2',
            product: [
                {
                    id : 1,
                    name: 'elephant',
                },
                {
                    id : 2,
                    name: 'horse',
                },
                {
                    id : 3,
                    name: 'lion',
                },
                {
                    id : 4,
                    name: 'tiger',
                }
            ]
        },
    ];

i want to store data inside product in to one array like xys = [ { id , name }, { id , name }... so on ] so i can call it like xyz.id, xyz.name etc

3 Answers

using flatmap

let categoriesxy= [
    {
        catid: 'category-1',
        product: [
            {
                id : 1,
                name: 'sparrow'
            },
            {
                id : 2,
                name: 'parrot',
            }
        ]

    },
    {
        catid: 'category-2',
        product: [
            {
                id : 1,
                name: 'elephant',
            },
            {
                id : 2,
                name: 'horse',
            },
            {
                id : 3,
                name: 'lion',
            },
            {
                id : 4,
                name: 'tiger',
            }
        ]
    },
];

let ans = categoriesxy.flatMap(({product}) => [...product])
console.log({ans})

you can can using map and flat methods

let categoriesxy= [
        {
            catid: 'category-1',
            product: [
                {
                    id : 1,
                    name: 'sparrow'
                },
                {
                    id : 2,
                    name: 'parrot',
                }
            ]

        },
        {
            catid: 'category-2',
            product: [
                {
                    id : 1,
                    name: 'elephant',
                },
                {
                    id : 2,
                    name: 'horse',
                },
                {
                    id : 3,
                    name: 'lion',
                },
                {
                    id : 4,
                    name: 'tiger',
                }
            ]
        },
    ];
    
    let products = categoriesxy.map((item)=> item.product).flat()
    console.log(products)

If you wish to also keep the category for each product you can do it like:

const categoriesxy = [ /* your list */ ]

const products = []
categoriesxy.forEach((category) => {
  category.product.forEach((product) => {
    products.push({ ...product, catid: category.catid })
  }
})
Related