2
How to read an array of objects and returns only objects that contain the same value in several attributes? I made this object array as an example.
var arrayDeObjetos = [
{"dia":1 , "desc":"Nigth","mes":1},
{"dia":1 , "Descricao":"Brilha","mes":1},
{"dia":2 , "desc":"Urna","mes":2},
{"dia":3 , "Descricao":"Covas","mes":1},
{"dia":2 , "desc":"Mario Corvas","mes":1},
{"dia":4 , "Desc":"Estrelas","mes":2}
];
In this Array I have several objects I wanted to group all and a new array that contains one object with all that contains dia = 1
and mes = 1
another object that contains all with dia = 2
and mes = 2
The end result has to be something like
var resultado = [
[
{"dia":1,"mes":1,"desc":"descricao AQI"},
{"dia":1,"mes":1,"desc":"Outra desc"}
],
[
{"dia":2,"mes":2,"desc":"outro mes"},
{"dia":1,"mes":1,"desc":"outra coisa"}
]];
There are "N" ways to do this, from iterations with foreach or for, or using the method map() > more information from him here: W3schools - Map. Or also, use a very good library that will make your life easier, it’s called Linqjs.
– Douglas Garrido
The result is the same or you meant that in each index of the array contains objects of the same day and same month?
– BrTkCa
Joao, in the
resultado
you have in questiondia 1
andmês 1
in both... it’s a mistake?– Sergio
@Sergio was my typo
– joao paulo santos almeida
Okay. Can you correct the question? I would like to give an answer but I want to be sure of the outcome you expect.
– Sergio