Answer the question
In order to leave comments, you need to log in
How to merge and check an array?
Faced such a problem.
I have a main array of data:
It is displayed on the main page of the application.
Array [
Object {
"idSite": 123456,
"status": "C",
"type": 1
},
Object {
"idSite": 123451,
"status": "O",
"type": 4
},
Object {
"idSite": 123458,
"status": "N",
"type": 1
},
Object {
"idSite": 123455,
"status": "C",
"type": 3,
}
]
[
{
"idSite": 123456,
"typetext": "System armed",
"status": "C"
},
{
"idSite": 123456,
"typetext": "System unarmed",
"status": "O"
},
{
"idSite": 123455,
"typetext": "Battery low",
"status": "C"
}
[
{
"idSite": 123456,
"typetext": "System armed",
"status": "C",
"type": 1
},
{
"idSite": 123456,
"typetext": "System unarmed",
"status": "O",
"type": 1
},
{
"idSite": 123455,
"typetext": "Battery low",
"status": "C",
"type": 3,
}
Answer the question
In order to leave comments, you need to log in
For technical reasons, on the backend side, there is no way to add the type value to the second array.
array2.forEach(el2 => {
el2.type = array1.find(el1 => el1.idSite === el2.idSite);
});
const array1Clone = array1.slice(0);
array2.forEach(el2 => {
const index = array1Clone.findIndex(el1 => el1.idSite === el2.idSite);
if (index > -1) {
el2.type = array1Clone.splice(index,1)[0].type;
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question