1
Hello, I have a two-dimensional array as follows
var array= [[{"id": 23, "nome": "Daniel"},
{"id": 24, "nome": "Bruno"},
{"id": 25, "nome": "Guilherme"}],
[{"id": 1, "nome": "Luiz"}],
[{"id": 2, "nome": "Miguel"},
{"id" :3, "nome": "Matheus"}]]
I need to print it out as follows
[{id: 23, nome: Daniel},{id: 1, nome: Luiz},{id: 2, nome: Miguel},
{id: 24, nome: Bruno},{id: 1, nome: Luiz},{id: 3, nome: Matheus},
{id: 25, nome: Guilherme},{id: 1, nome: Luiz},{id: 2, nome: Miguel}]
But all I got was something like
[{"id":23,"nome":"Daniel"},{"id":24,"nome":"Bruno"},{"id":25,"nome":"Guilherme"},{"id":1,"nome":"Luiz"},{"id":2,"nome":"Miguel"},{"id":3,"nome":"Matheus"}]
I did it this way:
for(i = 0; i < array.length; i++){
for(k = 0; k < array[i].length; k++){
console.log(array[i][k]);
}
}
Someone can give me a light?
Two questions:
1)
Why do you want duplicate array elements to appear? What is the logic?2)
what code did you try? you can add it to the question?– Sergio
In the above case it’s just an example, but I need it for a slide project that I’m working on, where each line would be a customer, and a customer may or may not have 2 or more slides and I need to slide one slide at a time to customers who have more than 1 slide.
– Daniel Candido
Okay, so what you want to do is
flatten
... move elements of nested arrays within arrays to the first level of the initial array. Correct?– Sergio
But that way he would print once each object right? I’m sorry if I’m saying nonsense.
– Daniel Candido
Just doing
flatten
he does not clean duplicates.– Sergio
Give an exact example of the final array you want. As it is I don’t understand the logic... why "Luiz" appears 3 times when there is only 1 in the initial array?
– Sergio
Note that in the first row I have 3 columns, in the second I have 1 column, I need to go through all rows and pick up one column at a time, where you have more than 1 column will not repeat, where you have only 1 will repeat.
– Daniel Candido