Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
Best way to replace a property in an array of objects
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

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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);

about 4 years ago · Juan Pablo Isaza Report

0

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; }

about 4 years ago · Juan Pablo Isaza Report

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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!