10

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.

6 Answers 6

9

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

2

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}

Comments

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

Comments

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)

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.