-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

4

3 Answers 3

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)

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

thanks a lot but with this result I'm sill missing the memberId and adminId from the original arrays.
@Cesar, By reading your answer I realize I have to again go through reduce operation.
I have updated my answer and now you can actually pass several arrays to merge by the unique key.
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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.