I have two arrays:
dataArray1 = [{
"id":1
"addressDetails": {55:1,56:20}
},
{
"id":2
"addressDetails": {55:30,56:10}
}
]
Above array contains addressDetails as object.
dataArray2= [{
"id":1
"addressDetails": [
{
"addressId": "55",
"city":"london",
"code":1
},
{
"addressId": "56",
"city":"paris",
"code":1
}
]
},
{
"id":2
"addressDetails": [
{
"addressId": "55",
"city":"london",
"code":0
},
{
"addressId": "56",
"city":"paris",
"code":0
}
]
}
]
This second array contains arrayDetails as array.
In both the arrays, id and addressId will be same. On the basis of both these id's I need to replace addressDetails object in dataArray1 by addressDetails array of dataArray2. In this replacement, I need to change value of "code" property of addressDetails array with the right hand side value(value on right side in addressDetailObject) for that particular id and addressId. For example, for Id "1" and addressId "55", in addressDetails object - "addressDetails": {55:1,56:10} value is "1", so I need to change value of "code" property in addressDetailsArrays with 1 and copy rest attritubtes as it is. How can I do that?
For this you can run
let result = array1.map((item) => {
//code to change item here
//find address value of array2 based on id and addressId
//replace the code with the address.code details you found above
});
What we are doing here is iterating over items of array1 and returning an updated value for each item.