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
You can just use
Object.assign()
:Complete snippet:
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:Can you just try this,
Here is an elegant way to do this: