简体   繁体   中英

add object value to an existing dotted key of an array

How to add object value to an array, while key is know and there is another value on that key, but I don't want to remove it. Also key is dotted

Here is an example

array = [key1:{value1},key2:{value3}];
key = this.props.name;

I want to get:

array = [key1:{value1},{value2},key2:{value3}];

Maybe it is better to change base object type to object or value to array?

If you want to store values with keys it's better use object and arrays as values:

obj = {
   key1: [value1],
   key2: [value3]
}

then if you want to update the object

obj[this.props.name] = [...obj[this.props.name], 'newItem']

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