6

I have array of objects:

var arr=[
{name:"d", op: "GTE", field:"ddd"},
{name:"dss", op: "LTE", field:"d4dd"},
{name:"dss", op: "GE", field:"ddddd"}]

I have managed to get index of every array in the element, however I'm not sure how to replace the whole object with new values? ex: at index 2, I want the object to look like this after update:

var arr=[
    {name:"d", op: "GTE", field:"ddd"},
    {name:"dss", op: "LTE", field:"d4dd"},
    **{name:"lithe", op: "GE", field:"34545"}**
]

how can i achieve this?

2
  • What is the change? Commented Aug 24, 2018 at 0:02
  • prop - name and field has changed Commented Aug 24, 2018 at 0:04

4 Answers 4

12

Just use simple array access and reassign at the index

arr[indexValue] = myNewObject
Sign up to request clarification or add additional context in comments.

Comments

4
arr[2] = {name:"lithe", op: "GE", field:"34545"}

Comments

3

You can assign the new element values to replace the existing one in the array. like:

 var newElement = {name:"lithe", op: "GE", field:"34545"};

 arr[2] = newElement;

See demo:

var arr = [{
    name: "d",
    op: "GTE",
    field: "ddd"
  },
  {
    name: "dss",
    op: "LTE",
    field: "d4dd"
  },
  {
    name: "dss",
    op: "GE",
    field: "ddddd"
  }
]
console.log("BEFORE");
console.log(arr);

var newElement = {name:"lithe", op: "GE", field:"34545"};

arr[2] = newElement;
/** 
if you need to loop thru the array and find our element, you can do this 

for(var idx=0; idx < arr.length; idx++){
  // any conditional here
  if( idx === 2 ){
     arr[idx] = newElement;
  }
}

*/
console.log("AFTER");
console.log(arr);

1 Comment

You can hide the snippet to make the answer concise and let readers focus on what's important. Open the snippet editor and scroll to the bottom of the panel on the left, there is a checkbox that says hide snippet by default. You can also hide it from the markdown by changing hide: false to hide: true. I've taken the liberty to do that for you. You can roll back if you don't like it.
1

As other answers mention, you can easily directly access and reassign, i.e. arr[2]={name:"lithe", op: "GE", field:"34545"}.

However, if you want to be more efficient (i.e. only some fields in the object at arr[2] need to be updated, as is your case), it would be better for you to merge your new object with the one that already exists at arr[2]. To do so, you may Object.assign(arr[2], {name:"lithe", field:"34545"}) (ES5), or arr[2]={...arr[2],...{name:"lithe", field:"34545"}} (ES6).

2 Comments

Your ES6 example doesn't work in Chrome 63. this is not the object being assigned to, it's the global window object (or whatever the context of the command is).
@Barmar Good catch! Fixed

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.