简体   繁体   中英

Push data into another object

How can I push new values into an existing object?

Basically I get some form data noteValue and I have some other data being input to my component person which I would like to make part of noteValue before I run my save function.

let noteValue = form.value;
noteValue.said_by.person_name = this.person.name;

This is an example I have working elsewhere but it doesn't seem to work:

let exValue = form.value;    
exValue['due_date'].timestamp = this.timestamp;

What am I missing? And is this even the right way to do this?

您可以使用spread operator创建一个同时包含prevStatenewData的新对象,然后将prevState替换为newState

const newState = {...prevState, newData} prevState = newState;

You can use Object.assign() .

Object.assign() Properties in the target object will be overwritten by properties in the sources if they have the same key.

 var obj1 = { foo: 'bar', x: 42 }; var obj2 = { foo: 'baz', y: 13 }; var obj = Object.assign({}, obj1, obj2); console.log(obj); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM