I have the following array of objects:
[
{
id: "1",
name: "ProdutoA",
categorias: [
{idCat: 1, name: "CategoriaA"},
{idCat: 2, name: "CategoriaB"}
]
},
{
id: "2",
name: "ProdutoB",
categorias: [
{idCat: 1, name: "CategoriaA"}
]
}
]
From this array of objects, I need to generate another array with the following structure:
[
{idCat: 1, name: "CategoriaA", products: ["ProdutoA","ProdutoB"]},
{idCat: 2, name: "CategoriaB", products: ["ProdutoA"]}
]
I do not know how to do this using the best JavaScript practices, only using the for
cycle, but I do not know if it will be performative when the first array is too large.
How to generate the second array as a function of the first?
Note : The initial array of objects will always be consistent and produced in this way through a framework. Therefore, all elements with the same idCat
will always have the same name
, so it is not necessary to worry about a hypothetical case where two elements with the same idCat
have name
s different.