skip to Main Content

I have the following two arrays:

let myArray = [{"id": 0, "creator":"Foo", "title":"title1", "message":"message1"}, {"id":1,"creator":"Bar", "title":"title", "message":"message2"}];

const payload = {"creator":"creator3", "title":"title3", "message":"message3"};

Knowing the index already, I would like to change only the creator, title and message parameters, leaving the id unchanged.

I have the following:

[myArray[0].creator, myArray[0].title, myArray[0].message] = [payload.creator, payload.title, payload.message];

I would like to know, is there a more elegant way to accomplish this? I was trying to somehow destructure this, but it looks like it may not be possible?

I would really appreciate any hints.

Thanks in advance.

6

Answers


  1. You can just use Object.assign():

    Object.assign(myArray[0], payload);
    

    Complete snippet:

    const myArray = [
      {"id": 0, "creator":"Foo", "title":"title1", "message":"message1"},
      {"id":1,"creator":"Bar", "title":"title", "message":"message2"}
    ];
    
    const payload = {"creator":"creator3", "title":"title3", "message":"message3"};
    
    Object.assign(myArray[0], payload);
    
    console.log(myArray);
    Login or Signup to reply.
  2. function updateObjectProperties(obj, newValues) {
      for (const key in newValues) {
        if (key in obj) {
          obj[key] = newValues[key];
        }
      }
    }
    let myArray = [{"id": 0, "creator":"Foo", "title":"title1", 
    "message":"message1"}, {"id":1,"creator":"Bar", "title":"title", 
    "message":"message2"}];
    
    const payload = {"creator":"creator3", "title":"title3", "message":"message3"};
    
    console.log(myArray[0]);
    updateObjectProperties(myArray[0], payload);
    console.log(myArray[0]);
    
    Login or Signup to reply.
  3. Your solution being a one-liner already can hardly be shortened any further. You could, however, write it in a different way, involving a .forEach() loop:

    ["creator","title","message"].forEach(p=>myArray[0][p]=payload[p])
    
    Login or Signup to reply.
  4. Using simple destructuring

    const myArray = [{"id": 0, "creator":"Foo", "title":"title1", "message":"message1"}, {"id":1,"creator":"Bar", "title":"title", "message":"message2"}];
    
    const payload = {"creator":"creator3", "title":"title3", "message":"message3"};
    
    const Changeindex = 0;
    
    myArray[Changeindex] = {...myArray[Changeindex],...payload};
    
    console.log(myArray)
    .as-console-wrapper { max-height: 100% !important; }
    Login or Signup to reply.
  5. Can you just try this,

    let myArray = [{"id": 0, "creator":"Foo", "title":"title1", 
    "message":"message1"}, {"id":1,"creator":"Bar", "title":"title",     "message":"message2"}];
    
    const payload = {"creator":"creator3", "title":"title3", "message":"message3"};
    
    const index = 0; // specify the index you want to update
    
    myArray[index] = {
      ...myArray[index], // keep other properties unchanged
      ...payload // update specified properties
    };
    
    Login or Signup to reply.
  6. Here is an elegant way to do this:

    const index = 0; // The index of the object you want to update
    const { creator, title, message } = payload;
    myArray[index] = { ...myArray[index], creator, title, message };
    
    let myArray = [
      {"id": 0, "creator":"Foo", "title":"title1", "message":"message1"},             {"id":1,"creator":"Bar", "title":"title", "message":"message2"}
    ];
    
    console.log("Original Array:");
    console.log(myArray);
    
    
    const payload = {"creator":"creator3", "title":"title3", "message":"message3"};
    
    const index = 0; // The index of the object you want to update
    const { creator, title, message } = payload;
    myArray[index] = { ...myArray[index], creator, title, message };
    
    console.log("Updated Array:");
    console.log(myArray);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search