skip to Main Content

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


  1. To concat 2 arrays, you can use (as the name suggests) the .concat() function like this.

    const foo = [1, 2];
    const bar = [3];
    const concatedArray = foo.concat(bar) //--> [1,2,3]
    

    To sort the array afterwards, you can use the .sort() function.

    const arrayOfObjects = [
      { id: 3, name: 'Alice' },
      { id: 1, name: 'Bob' },
      { id: 2, name: 'Charlie' },
    ];
    
    // Sort the array by the 'id' property
    arrayOfObjects.sort((a, b) => a.id - b.id);
    
    console.log(arrayOfObjects);
    
    // output
    //[
    //  { id: 1, name: 'Bob' },
    //  { id: 2, name: 'Charlie' },
    //  { id: 3, name: 'Alice' }
    //]
    

    I hope this answers your question.

    Login or Signup to reply.
  2. 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:

    getData()
    {
      return forkJoin([
           this.httpClient.get(...),
           this.httpClient.get(...)]
         ).
         pipe.pipe(
            map(([res1, res2]: [any[], any[]]) =>
            res1.concat(res2)
                .sort((a: any, b: any) => a.lineLossId - b.lineLossId)
        )
      )
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search