Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

201
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda