简体   繁体   中英

How to add a key value pair to an array of objects in javascript with multiple value?

I am able to have an object like this

var obj = {key1: value1, key2: value2};

but i need an object having multiple value how can i achieve it? for example

var obj = {key1:{ value1,value2}, key2: {value3,value4,value5}};

An object can have properties. And the property is implementation of key value pair in javascript. If you want to have keys in an object, you need to put value for each key.

In example of yours,

var obj = {key1:{ value1,value2}, key2: {value3,value4,value5}};

sholud be

var obj = {
           key1:{ value1:null, value2:null }, 
           key2: {value3:null, value4: null, value5: null}
           };

so that, you can the directly access key in obj like obj.key1.value1 Additionally you can get keys Object.keys(obj); => ['key1', 'key2'] and Object.keys(obj.key1); => ['value1', 'value2']

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