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