const organisations = {
fetchingData : false ,
error : null,
data : {
totalCount : 68,
data : [
{ indID : 12345, name : 'abc'},
{ indID: 12, name : 'xyz'}]
}
}
const checkedBy = {
data : {
name : 'PQ',
},
indID : 12345,
}
I would like to replace the name property inside the organisations object by the name property of checkedBy object when indID matches with the array of the data array of the organisations object
I think this is the mapper function you want?
let org = {
fetchingData : false ,
error : null,
data : {
totalCount : 68,
data : [
{ indID : 12345, name : 'abc'},
{ indID: 12, name : 'xyz'}]
}
}
const checkedBy = {
data : {
name : 'PQ',
},
indID : 12345,
}
console.log("Before :: \n", org)
org.data.data = org.data.data.map(x => {
if(x.indID === checkedBy.indID){
x.name = checkedBy.data.name
}
return x;
})
console.log("After :: \n", org);
You can easily achieve the result using find
const organisations = {
fetchingData: false,
error: null,
data: {
totalCount: 68,
data: [
{ indID: 12345, name: "abc" },
{ indID: 12, name: "xyz" },
],
},
};
const checkedBy = {
data: {
name: "PQ",
},
indID: 12345,
};
const obj = organisations.data.data.find((o) => o.indID === checkedBy.indID);
obj.name = checkedBy.data.name;
console.log(organisations);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
you can mutate the organisations.data.data in the following way.
const organisations = {
fetchingData: false,
error: null,
data: {
totalCount: 68,
data: [{
indID: 12345,
name: 'abc'
},
{
indID: 12,
name: 'xyz'
}
]
}
}
const checkedBy = {
data: {
name: 'PQ',
},
indID: 12345,
}
//replace name
organisations.data.data = organisations.data.data.map(org => {
if (org.indID === checkedBy.indID) {
return {
...org,
// replace name with checked by name
name: checkedBy.data.name
}
}
return org
})
console.log(organisations.data.data)