3
I have the following object array:
[
{
id: "1",
name: "ProdutoA",
categorias: [
{idCat: 1, name: "CategoriaA"},
{idCat: 2, name: "CategoriaB"}
]
},
{
id: "2",
name: "ProdutoB",
categorias: [
{idCat: 1, name: "CategoriaA"}
]
}
]
From this object array, I need to generate another array with the following structure:
[
{idCat: 1, name: "CategoriaA", products: ["ProdutoA","ProdutoB"]},
{idCat: 2, name: "CategoriaB", products: ["ProdutoA"]}
]
I don’t know how to do this using Javascript best practices, only using the cycle for
, but I don’t know if it will be performatic when the first array is very large.
How to generate the second array according to the first?
Observing: The initial object array will always be consistent and is produced this way through a framework. So, all elements with the same idCat
will always have the same name
, and therefore it is not necessary to worry about a hypothetical case where two elements with the same idCat
have name
s different.
Could you define too big? for example, an array with 1000 positions may be large for a human to process visually, but for a computer it is small.
– Tobias Mesquita
But since your idea is to create a new array based on the first one, then the ideal is to use Array.prototype.map in place of for.
– Tobias Mesquita
And what should happen if you have
{categoris : [ {idCat: 1, name : 'CategoriaA'}]}
and another with{categoris : [ {idCat: 1, name : 'CategoriaB'}]}
?– Guilherme Lautert
@Tobymosque imagine that it is 1000 positions, but in "categories" I have 10 records, 10 thousand records processed, the problem is that I have to check the new array every time I want to check if the category already exists.
– Filipe Moraes
@Guilhermelautert this cannot occur, there are no different categories with the same ID, at least that’s what you used as an example, notice that "Categoriaa" has the same "idCat" as "Categoriab".
– Filipe Moraes
@Filipemoraes are you agree that
name
is irrelevant andidCat
who should point to the respective category?– Guilherme Lautert
@Filipemoraes I think what Guilherme means is that his structure "violates the third normal form" hehe. For if in
categorias
you already haveidCat
andnome
, Why repeat the same parameters on each product, why not just have a list of Ids in the product? I understand that maybe this data is being generated by a tool, and so cannot be changed, so in this case we can assume that a solution does not need to worry about it?– mgibsonbr
@Guilhermelautert yes, that’s it, but the name has to be included in the new array, but the connection is done via idCat.
– Filipe Moraes
@mgibsonbr yes, the first array is generated by a framework, the second array is what I need, so the need to transform it.
– Filipe Moraes
P.S. The underscore.js library has a method
groupBy
that could help you with this, but the output it generates is another (it would have to be adapted), and anyway it does not pay to include a library only to use a function, especially when seeking efficiency. Better do it by hand anyway...– mgibsonbr
In short you want to go through the whole
array verificando quais produtos estão vinculado com cada
idCat`– Guilherme Lautert
@Exact Guilhermelautert, according to the example I posted.
– Filipe Moraes
@mgibsonbr then, my doubt is how to do this by hand without the code getting heavy, that’s my concern! I can do it using only the FOR cycle.
– Filipe Moraes
@Filipemoraes I don’t think using the
for
is bad, especially if your logic is correct. I wrote a response usingforEach
, but it would be the same (and even more efficient, I think) usingfor
s, what matters is that the number of operations executed - as far as my knowledge goes - is minimal, you could hardly achieve a more efficient solution...– mgibsonbr
This question is in the closing row as "is not clear enough". To me she seems to be clear, so I’m going to vote on "Leave open". Could someone there explain why they want to shut it down?
– Victor Stafusa
@Victorstafusa I voted, because in the 3,5,6,7,8 comments that only then can get the result as expected, without considering this, and expected response would not be possible. Then it would be better to point them out in the question.
– Guilherme Lautert
@Guilhermelautert Ok, I edited the question to try to cover this case based on what is written in the comments.
– Victor Stafusa
@Guilhermelautert Looking at the example I gave, all questions 3,5,6,7 and 8 are answered. In the example it is possible to see that the connection is by idCat and that the category name is not repeated. The question is clear, how to turn an array A into an array B.
– Filipe Moraes