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

117
Vistas
How to find and replace an object with in array of objects

I have an array of objects like this.

 [{
    "id": 1,
    "name": "January",
    "abc": abc,
    "xyz": xyz
}, {
    "id": 2,
    "name": "February",
    "abc": abc,
    "xyz": xyz
}]

I want to replace the object which is having id 2 with the different object and i want to have my object like this .

 [{
    "id": 1,
    "name": "January",
    "abc": abc,
    "xyz": xyz
}, {
    "id": 2,
    "name": "New month",
    "abc": 1234abc,
    "xyz": someVlaue
}]

how to do it in efficient way in typescript or javascript.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Different ways to achieve this.

  1. By using Object.assign() method. It returns the modified target object.

const data = [{
    "id": 1,
    "name": "January",
    "abc": "abc",
    "xyz": "xyz"
}, {
    "id": 2,
    "name": "February",
    "abc": "abc",
    "xyz": "xyz"
}];

const target = data.find((obj) => obj.id === 2);

const source = {
  id: 2,
  name: 'New Month',
  abc: 'abc123',
  xyz: 'someValue'
};

Object.assign(target, source);

console.log( data );

  1. By using array.map() method which creates a new array populated with the results of calling a provided function on every element in the calling array.

const data = [{"id": 1,"name": "January","abc": "abc","xyz": "xyz"}, {"id": 2,"name": "February","abc": "abc","xyz": "xyz"}];

const modifiedObj = {"id": 2,"name": "New month","abc": "1234abc","xyz": "someVlaue"};

const result = data.map((item) => item.id === modifiedObj.id ? modifiedObj : item);

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Looks like this was explained well in this post. The top answer explained how to use find(), as well as findIndex(). Should help you achieve what you are looking to do.

Find object by id in an array of JavaScript objects

EDIT: Forgot about the replacement piece.

Replace a particular object based on id in an array of objects in javascript

about 4 years ago · Juan Pablo Isaza Denunciar

0

Another way to replace the object:

const data = [{"id": 1,"name": "January","abc": "abc","xyz": "xyz"}, {"id": 2,"name": "February","abc": "abc","xyz": "xyz"}];

const newObj = {"id": 2,"name": "New month","abc": "1234abc","xyz": "someVlaue"};
const targetId = 2;

const result = data.map((obj) => obj.id === targetId ? newObj : obj);
console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

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