-3

I have two arrays of objectcs

const a = [
  {
    "categoryId": 1,
    "categoryName": "category 1",
    "id": 11
  },
  {
    "categoryId": 2,
    "teamName": "category 2",
    "id": 22
  }
]

const b = [
  {
    "categoryId": 2,
    "categoryName": "category 2",
    "id": 33
  },
  {
    "categoryId": 3,
    "categoryName": "category 3",
    "id": 44
  }
]

now I want to merge them together that the end result should look like:

const result = [
  {
    "categoryId": 1,
    "categoryName": "category 1",
    "aId": 11,
    "bId" null: 
  },
  {
    "categoryId": 2,
    "categoryName": "category 2",
    "aId": 22,
    "bId" 33:
  },
  {
    "categoryId": 3,
    "categoryName": "category 3",
    "aId": null,
    "bId" 44:
  }
]

I don't see how I can assign the null values.

I've already tried it with Object.assign and with the "..." operator but don't get the result like wished.

In the same way I want to rename the id's to a new key depending on their array.

let result = a.concat(b) would give me a wrong result

tobtherush
  • 87
  • 2
  • 7
  • Iterate over 2nd array and use Object.assign to merge items – deathangel908 Jan 22 '19 at 09:17
  • Can you add what you tried? – Avanthika Jan 22 '19 at 09:19
  • 1
    Possible duplicate of [How to merge two arrays in JavaScript and de-duplicate items](https://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items) – Yasser Shaikh Jan 22 '19 at 09:20
  • Possible duplicate of [change property name](https://stackoverflow.com/questions/8483425/change-property-name) and [JavaScript merging objects by id](https://stackoverflow.com/questions/19480008/javascript-merging-objects-by-id) – str Jan 22 '19 at 09:27

3 Answers3

2

You can use the contat() to merge the arrays together and then reduce() to remove repeated objects by its key:

    const admin = [
      {
        "categoryId": 66,
        "categoryName": "category 66",
        "id": 204
      },
      {
        "categoryId": 149,
        "teamName": "category 149",
        "id": 178
      }
    ]

    const member = [
      {
        "categoryId": 66,
        "teamName": "category 66",
        "id": 271
      },
      {
        "categoryId": 68,
        "teamName": "category 68",
        "id": 264
      }
    ];
    
    const client = [
      {
        "categoryId": 66,
        "teamName": "category 66",
        "id": 271
      },
      {
        "categoryId": 155,
        "teamName": "no team",
        "id": 264
      }
    ];
    const merge = function(...params) {
        let mergeResult = [];
        params.map(elem => {mergeResult = mergeResult.concat(elem); });
        return mergeResult;
    }

    const result = Object.values(merge(member, admin, client).reduce((acc,cur)=>Object.assign(acc,{[cur.categoryId]:cur}),{}));

    console.log(result);

If you need to change the repeated filter criteria just change cur.categoryId in the Object.assign() function

César Ferreira
  • 681
  • 1
  • 5
  • 12
2

You can use a Set to get just the unique ids, then you can map over that, finding the matching data and building the object in the required format:

const admin = [{
    "categoryId": 66,
    "categoryName": "category 66",
    "id": 204
  },
  {
    "categoryId": 149,
    "teamName": "category 149",
    "id": 178
  }
]

const member = [{
    "categoryId": 66,
    "teamName": "category 66",
    "id": 271
  },
  {
    "categoryId": 68,
    "teamName": "category 68",
    "id": 264
  }
]

const findCategory = (categories, category) => categories
  .find(x => x.categoryId === category) || null;

const mergeCategories = (adminIn, memberIn) => {
  const mergedCategories = [
    ...adminIn,
    ...memberIn,
  ];
  
  // we would like to know just the unique id's
  const categoryIds = [...new Set(mergedCategories.map(x => x.categoryId))];
  
  // we can map each unique id
  return categoryIds.map(categoryId => {
  
    // then we can find whether it has a matching category for each
    const adminCategory = findCategory(admin, categoryId);
    const memberCategory = findCategory(member, categoryId);
  
    // now we can use that data to build the object in the required format
    return {
      categoryId,
      categoryName: `category ${categoryId}`,
      adminId: adminCategory === null ? adminCategory : adminCategory.id,
      memberId: memberCategory === null ? memberCategory : memberCategory.id
    }
  });

}

const result = mergeCategories(
  admin,
  member,
)

console.dir(result)
OliverRadini
  • 6,238
  • 1
  • 21
  • 46
1

forEach loop can be used and we can assign values accordingly

const admin = [
    {
      "categoryId": 66,
      "categoryName": "category 66",
      "id": 204
    },
    {
      "categoryId": 149,
      "teamName": "category 149",
      "id": 178
    }
  ]
  
  const member = [
    {
      "categoryId": 66,
      "teamName": "category 66",
      "id": 271
    },
    {
      "categoryId": 68,
      "teamName": "category 68",
      "id": 264
    }
  ]
var arr=[];
  var k=admin.forEach((e)=>{
    var a=e.categoryId;
   if(e.teamName)
   {
e.categoryName=e.teamName;
delete e.teamName;
   }
   if(e.id)
e.adminid=e.id;
else
e.id=null
e.memberId=null;
delete e.id
var t=member.forEach((x)=>{
  if(x.categoryId==a)
  e.memberId=x.id;

})
arr.push(e);
  })
console.log(arr)
ellipsis
  • 12,049
  • 2
  • 17
  • 33