I have two arrays and want to push the values from the second array having the same property value. Comparing the values by using addrSeq
property and push the matching object in to vpainfo
array
Below are the sample JSON structure,
Array1:
[
{
"addrSeq": "12",
"accountMask": "********7479",
"vpainfo": []
},
{
"addrSeq": "13",
"accountMask": "********74",
"vpainfo": []
}
]
Array2:
[
{
"addrSeq": "12",
"vpa": "saasasa@fff"
},
{
"addrSeq": "12",
"vpa": "xyz@com"
},
{
"addrSeq": "13",
"vpa": "saas@ddd"
}
]
CodePudding user response:
you just need to learn javascript, here is result please look into this i just store both into array, and then run loop and match and push into one.
let firstarray =[
{
"addrSeq": "12",
"accountMask": "********7479",
"vpainfo": []
},
{
"addrSeq": "13",
"accountMask": "********74",
"vpainfo": []
}
]
let secondarray = [
{
"addrSeq": "12",
"vpa": "saasasa@fff"
},
{
"addrSeq": "12",
"vpa": "xyz@com"
},
{
"addrSeq": "13",
"vpa": "saas@ddd"
}
]
// merge both array into one using addrSeq match value
let mergedarray = firstarray.map(function(item) {
secondarray.map(function(item2) {
if (item.addrSeq == item2.addrSeq) {
item.vpainfo.push(item2);
}
});
return item;
});
console.log(mergedarray);
CodePudding user response:
You can achieve it via simple forEach
loop.
Demo :
const array1 = [
{
"addrSeq": "12",
"accountMask": "********7479",
"vpainfo": []
},
{
"addrSeq": "13",
"accountMask": "********74",
"vpainfo": []
}
];
const array2 = [
{
"addrSeq": "12",
"vpa": "saasasa@fff"
},
{
"addrSeq": "12",
"vpa": "xyz@com"
},
{
"addrSeq": "13",
"vpa": "saas@ddd"
}
];
array2.forEach((obj) => {
array1.forEach((array1Obj) => {
if (obj.addrSeq === array1Obj.addrSeq) {
array1Obj.vpainfo.push(obj.vpa)
}
});
});
console.log(array1);
CodePudding user response:
Assuming that the OP requires Array1
to be updated such that each object's vpaIinfo
array gets updated based on the data in Array2
, the following solution may be one possible way to achieve the desired objective.
Code Snippet
const getUpdatedArr = (ar1, ar2) => (
ar1.map(
obj => ({
...obj,
vpainfo: [
...obj.vpainfo,
...ar2.filter(
({addrSeq}) => (obj.addrSeq === addrSeq)
).map(
({vpa}) => vpa
)
]
})
)
);
const arr1 = [
{
"addrSeq": "12",
"accountMask": "********7479",
"vpainfo": []
},
{
"addrSeq": "13",
"accountMask": "********74",
"vpainfo": []
}
];
const arr2 = [
{
"addrSeq": "12",
"vpa": "saasasa@fff"
},
{
"addrSeq": "12",
"vpa": "xyz@com"
},
{
"addrSeq": "13",
"vpa": "saas@ddd"
}
];
console.log(getUpdatedArr(arr1, arr2));
Explanation
- Iterate over array-1 using
.map()
- For each object, keep the rest of the props as-is & manipulate only
vpainfo
- Retain any existing values in current
vpainfo
(using...obj.vpainfo
) - Append to this, any relevant
vpa
found in array-2 - The above is accomplished by using
.filter()
to retain only those with sameaddrSeq
-AND- then, using.map()
to extract just thevpa
prop (using de-structuring)
CodePudding user response:
Possible one liner using map
and filter
let array1 = [{"addrSeq": "12","accountMask": "********7479","vpainfo": []},{"addrSeq": "13","accountMask": "********74","vpainfo": []}];
const array2 = [ { "addrSeq": "12", "vpa": "saasasa@fff" }, { "addrSeq": "12", "vpa": "xyz@com" }, { "addrSeq": "13", "vpa": "saas@ddd" }];
array1 = array1.map(({addrSeq,accountMask}) => ({addrSeq,accountMask,vpainfo: array2.filter((e) => e.addrSeq === addrSeq)}))
console.log(array1)
CodePudding user response:
Logic
- Loop through first array
- Find the matching items from second array by comparing the
addrSeq
values. - Update the
vpainfo
of item in first array as the concatenated value ofvpa
s from second array.
Working Fiddle
const arr1 = [
{ "addrSeq": "12", "accountMask": "********7479", "vpainfo": [] },
{ "addrSeq": "13", "accountMask": "********74", "vpainfo": [] }
]
const arr2 = [
{ "addrSeq": "12", "vpa": "saasasa@fff" },
{ "addrSeq": "12", "vpa": "xyz@com" },
{ "addrSeq": "13", "vpa": "saas@ddd" }
]
arr1.forEach((node) => node.vpainfo = node.vpainfo.concat(arr2.filter(item => item.addrSeq === node.addrSeq).map(item => item.vpa)))
console.log(arr1);