2

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.

Roster
  • 1,764
  • 6
  • 17
  • 36

6 Answers6

5

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);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
1

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

Mizzy
  • 71
  • 6
1
const arr =  [{
  "id": 1,
  "name": "January",
  "abc": "abc",
  "xyz": "xyz"
}, {
  "id": 2,
  "name": "February",
  "abc": "abc",
  "xyz": "xyz"
}]

const index = arr.findIndex(entry => entry.id === 2);
arr[index] = {id: 2, name: "New month", abc: "1234abc", xyz: "someVlaue"} // (sic)
JS Chewy
  • 71
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 17 '22 at 20:02
1

You can use Object.assign() with find() as follows:

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

Object.assign(
    //find the desired object
    data.find(({id,name,abc,xyz}) => id === 2),
    //pass these new values 
    {name:"New Month",abc:"abc123",xyz:"someValue"}
);

console.log( data );
PeterKA
  • 24,158
  • 5
  • 26
  • 48
1

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}
A1exandr Belan
  • 4,442
  • 3
  • 26
  • 48
0

Assign works, but you can also just use find then update the value. If your data structure is exactly like you have it, then assign will work if you dont want to mutate the original value. However, if you have objects as any of the values then you might as well use this approach unless you want to also do a deep copy of the entire object before changing it.

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

const element = data.find(el => el.id === 2)
element.xyz = "WHATERVER"
console.log(data)
terpinmd
  • 1,014
  • 8
  • 15