0

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

2
  • You can loop it through using map or forEach. and push into another array. Commented Sep 13, 2022 at 13:22
  • 1
    Please share what you have tried so far as a text-based minimal reproducible example Commented Sep 13, 2022 at 13:22

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

this answer is more efficient than use map and flat separately. and in your answer you don't need to use spread operator in flatMap
1

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)

Comments

1

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

3 Comments

ur ans is correct but u have made typo error plz edit it
I see more optimized and shorter answers below, its the boring answer that got verified
@asma seems like he also wanted the catid to be present. Otherwise map + flat is a better option

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.