I have two array of objects, in which if id
and aid
property values match then append the property code
to arr1
and return the result
var arr1 = [
{ id: 1, name: "xxx", cn: "IN" },
{ id: 2, name: "yyy", cn: "MY" },
{ id: 3, name: "zzz", cn: "SG" },
]
var arr2 = [
{ aid: 1, code: "finance" },
{ aid: 2, code: "others" },
{ aid: 4, code: "finance" },
{ aid: 5, code: "product" },
]
Expected result:
var arr1 = [
{ id: 1, name: "xxx", cn: "IN", code: 'finance'},
{ id: 2, name: "yyy", cn: "MY", code: 'others'},
{ id: 3, name: "zzz", cn: "SG", code: ''},
]
I tried
var result = arr1.map(e=> ({
...e,
code: arr2.map(i=>i.code)
})
CodePudding user response:
You can do what you require with a loop over arr1
looking for matches in arr2
using filter()
. This will add the code
property to the existing arr1
array without creating a new array instance.
Note that this logic assumes that there will only ever be 0 or 1 matches in arr2
.
var arr1 = [
{ id: 1, name: "xxx", cn: "IN" },
{ id: 2, name: "yyy", cn: "MY" },
{ id: 3, name: "zzz", cn: "SG" },
]
var arr2 = [
{ aid: 1, code: "finance" },
{ aid: 2, code: "others" },
{ aid: 4, code: "finance" },
{ aid: 5, code: "product" },
]
arr1.forEach(o1 => o1.code = arr2.filter(o2 => o2.aid === o1.id)[0]?.code || '');
console.log(arr1);
CodePudding user response:
I am not sure if this is the most optimal solution but this definitely works for your case:
let result = arr1.map((outerObj) => {
// First we try to find the index of the matching object
let index = arr2.findIndex((innerObj) => {
return outerObj.id == innerObj.aid;
});
// In case of no match, set case as "" as per requirement
if(index == -1) {
outerObj.code = "";
return outerObj;
}
// If a match is found, then combine the two objects.
let mergedObj = { ...outerObj, ...arr2[index]};
// aid is not needed in the result, Who wants aids anyway?
delete mergedObj.aid;
return mergedObj;
});
If not more optimal, then definitely there could be smaller code but I think the code should be clear and readable rather than being smaller.
CodePudding user response:
First, massage the data into a format for fast manipulation; then manipulate.
The following:
- Puts every item in
arr2
into a map, keyed onaid
- Enumerates
arr1
once, looking into the map for each item to see if there is a corresponding code
Performance: O(N).
var arr1= [
{id:1, name: "xxx", cn: "IN"},
{id:2, name: "yyy", cn: "MY"},
{id:3, name: "zzz", cn: "SG"} ]
var arr2 = [
{ aid: 1, code: "finance"},
{ aid: 2, code: "others"},
{ aid: 4, code: "finance"},
{ aid: 5, code: "product"} ]
const map = arr2.reduce((p,c) => p.set(c.aid, c), new Map)
arr1.forEach((i) => i.code = map.get(i.id)?.code || '')
console.log(arr1)
CodePudding user response:
You need to first find matching item in arr2
and them create the composed item.
Try like this:
var arr1 = [
{ id: 1, name: "xxx", cn: "IN" },
{ id: 2, name: "yyy", cn: "MY" },
{ id: 3, name: "zzz", cn: "SG" },
];
var arr2 = [
{ aid: 1, code: "finance" },
{ aid: 2, code: "others" },
{ aid: 4, code: "finance" },
{ aid: 5, code: "product" },
];
const result = arr1.map((item) => {
const match = arr2.find((o) => o.aid === item.id);
return { ...item, code: match ? match.code : '' };
});
console.log(result);
CodePudding user response:
we can get like this too
var arr1 = [
{ id: 1, name: "xxx", cn: "IN" },
{ id: 2, name: "yyy", cn: "MY" },
{ id: 3, name: "zzz", cn: "SG" },
]
var arr2 = [
{ aid: 1, code: "finance" },
{ aid: 2, code: "others" },
{ aid: 4, code: "finance" },
{ aid: 5, code: "product" },
]
const fun = (ar, ar2)=>{
const getResult = ar.map((e)=> {
const Data ={
id : e.id,
name : e.name,
cn : e.cn,
code : ar2.find((e2)=> e2.aid===e.id)?.code || ""
}
return Data;
})
return getResult
}
console.log(fun(arr1, arr2))
CodePudding user response:
we can usefind
method too
var arr1 = [
{ id: 1, name: "xxx", cn: "IN" },
{ id: 2, name: "yyy", cn: "MY" },
{ id: 3, name: "zzz", cn: "SG" },
]
var arr2 = [
{ aid: 1, code: "finance" },
{ aid: 2, code: "others" },
{ aid: 4, code: "finance" },
{ aid: 5, code: "product" },
]
arr1.forEach(e => e.code = arr2.find(d => d.aid === e.id)?.code || '');
console.log(arr1);