I got two arrays with some objects which I receive from API calls.
I need to combine those in one single array, but some objects are in different states on my app. So, if the objects in both array got the same "Code" attribute, I need to keep the one with more information.
I tried to make and nested loop (yeah, from what I read it's a poor technique, but I will never get more than 5 items at the same time). Then I tried to compare both "code objects", and push the equal results on a new array, which works fine. When I try to use a second condition on the same loop, it just doesn't work.
let array1 = [{
'DataCad': "2019-01-04T15:04:02.663",
'Field1': "rt",
'Code': "DFG3456",
},
{
'DataCad': "2019-01-07T11:37:31.8",
'Field1': "TESTE2",
'Code': "TYU1235",
},
{
'DataCad': "2019-01-07T13:15:48.97",
'Field1': "Uppercase",
'Code': "JJJ1212",
},
{
'DataCad': "2019-01-07T16:35:32.697",
'Field1': "234",
'Code': "OOO1111",
},
{
'DataCad': "2019-01-07T10:46:46.437",
'Field1': "TESTE1",
'Code': "GHJ1234",
}
]
let array2 = [{
'DataAge': "2019-01-07",
'DataCad': "2019-01-04T15:04:49.05",
'HoraAge': "12:25",
'Field1': "rt",
'Pag': "N",
'Code': "DFG3456",
},
{
'DataAge': "2019-01-07",
'DataCad': "2019-01-07T11:17:57.583",
'HoraAge': "13:15",
'Field1': "TESTE1",
'Pag': "N",
'Code': "GHJ1234",
},
{
'DataAge': "2019-01-08",
'DataCad': "2019-01-07T11:38:46.08",
'HoraAge': "10:15",
'Field1': "TESTE2",
'Pag': "N",
'Code': "TYU1235",
},
{
'DataAge': "2020-01-7 ",
'DataCad': "2019-01-07T13:16:00.567",
'HoraAge': "15:15",
'Field1': "Uppercase",
'Pag': "N",
'Code': "JJJ1212",
}
]
let finalArray = [];
for (var i in array1) {
for (var j in array2) {
if (array1[i].Code == array2[j].Code && !array1[i].DataAge) {
finalArray.push(array1[i].Code)
}
}
}
console.log(finalArray);
}
Need to get one single array with those filtered results. Is there a better solution?
more information, like more properties? What if two items have the same number of properties and same code?