Search code examples
javascriptobjectdata-structuresmergeconcatenation

How to merge merge two objects into one and also update total count of records in merged object


I need to merge two objects. I've tried to use the concat method on the items arrays but it does not fit my needs due to additional properties like e.g. count: 5, total_records : "3", offset: 500, has_more: false being present in my object.

I want to retain such properties in my merged object as well. And I especially want to keep track of the count of total objects/records in the merged items array by updating the merged object's count and total_records values.

As for the below example code, in case the first object's items array contains 3 items and the second object's items array contains just 2, then the final merged object in addition to the concatenated items array should show ...

count: 5,
total_records: "5",
offset: 500,
has_more: false,

Please suggest how to implement this. Below is my example data.

const arr1 = {
  items: [
    { content_sys_id: "15b9d20b87941d10f2d740c8dabb35f1" },
    { content_sys_id: "009e86a787dc5d10f2d740c8dabb35c8" },
    { content_sys_id: "21f5b2d597a151d0da8bd714a253af44" },
  ],
  count: 3,
  total_records: "3",
  offset: 500,
  has_more: false,
};
const arr2 = {
  items: [
    { content_sys_id: "002301478788d15038a740c8dabb350e" },
    { content_sys_id: "cb895ec787941d10f2d740c8dabb357e" },
  ],
  count: 2,
  total_records: "2",
  offset: 500,
  has_more: false,
};

And the expected result would be ...

{
  items: [
    { content_sys_id: "15b9d20b87941d10f2d740c8dabb35f1" },
    { content_sys_id: "009e86a787dc5d10f2d740c8dabb35c8" },
    { content_sys_id: "21f5b2d597a151d0da8bd714a253af44" },
    { content_sys_id: "002301478788d15038a740c8dabb350e" },
    { content_sys_id: "cb895ec787941d10f2d740c8dabb357e" },
  ],
  count: 5,
  total_records: "5",
  offset: 500,
  has_more: false,
}

Solution

  • For fast integration

    const arr1 = {
      items: [
        { content_sys_id: "15b9d20b87941d10f2d740c8dabb35f1" },
        { content_sys_id: "009e86a787dc5d10f2d740c8dabb35c8" },
        { content_sys_id: "21f5b2d597a151d0da8bd714a253af44" },
      ],
      count: 3,
      total_records: "3",
      offset: 500,
      has_more: false,
    };
    const arr2 = {
      items: [
        { content_sys_id: "002301478788d15038a740c8dabb350e" },
        { content_sys_id: "cb895ec787941d10f2d740c8dabb357e" },
      ],
      count: 2,
      total_records: "2",
      offset: 500,
      has_more: false,
    };
    
    console.log({
        items: [...arr1.items, ... arr2.items],
        count: arr1.count + arr2.count,
        total_records: (arr1.count + arr2.count).toString(),
        offset: arr1.offset,
        has_more: arr1.has_more || arr2.has_more
    });
    .as-console-wrapper { min-height: 100%!important; top: 0; }

    And with an undeniable source of truth

    const arr1 = {
      items: [
        { content_sys_id: "15b9d20b87941d10f2d740c8dabb35f1" },
        { content_sys_id: "009e86a787dc5d10f2d740c8dabb35c8" },
        { content_sys_id: "21f5b2d597a151d0da8bd714a253af44" },
      ],
      count: 3,
      total_records: "3",
      offset: 500,
      has_more: false,
    };
    const arr2 = {
      items: [
        { content_sys_id: "002301478788d15038a740c8dabb350e" },
        { content_sys_id: "cb895ec787941d10f2d740c8dabb357e" },
      ],
      count: 2,
      total_records: "2",
      offset: 500,
      has_more: false,
    };
    
    const newArray = [...arr1.items, ... arr2.items]
    console.log({
        items: newArray,
        count: newArray.length,
        total_records: newArray.length.toString(),
        offset: arr1.offset,
        has_more: arr1.has_more || arr2.has_more
    });
    .as-console-wrapper { min-height: 100%!important; top: 0; }