i am calling two api at same time and getting below response in array1 and array2.at
Now i wanted to merge all the object in one array
Below is my array1 and array2 response.
Const Array1 = [
{
"lineLossId": 1,
"dataSource": "DFOS",
"category": "Dressings",
"entryDetails": {
"month": "September",
"year": "2023"
},
"HrDbMonth": null
},
{
"lineLossId": 2,
"dataSource": "DFOS",
"entryDetails": {
"month": "August",
"year": "2023"
},
"HrDbMonth": null
}
];
const array2 = [
{
"lineLossId": 3,
"dataSource": "DFOS",
"category": "Dressings",
"entryDetails": {
"month": "August",
"year": 2023
},
"uom": "OOOUnits"
}
];
I am expecting all the response to be merge in one array with all objects and in sequential index key.
Below is my Expected output
Const array3 = [
{
"lineLossId": 1,
"dataSource": "DFOS",
"category": "Dressings",
"entryDetails": {
"month": "September",
"year": "2023"
},
"HrDbMonth": null
},
{
"lineLossId": 2,
"dataSource": "DFOS",
"entryDetails": {
"month": "August",
"year": "2023"
},
"HrDbMonth": null
},
{
"lineLossId": 3,
"dataSource": "DFOS",
"category": "Dressings",
"entryDetails": {
"month": "August",
"year": 2023
},
"uom": "OOOUnits"
}
];
2
Answers
To concat 2 arrays, you can use (as the name suggests) the
.concat()
function like this.To sort the array afterwards, you can use the
.sort()
function.I hope this answers your question.
If you need call to two (or more) diferents APIs and one is not depending another, you use forkJoin rxjs operator to have an unique subscription. after use map operator to return the join of the two response. You can also use sort method of the array to be sure the response get the array sorted: