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

115
Views
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 answers
Answer question

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 Report

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 Report

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 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!