-1

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}};
Priya Ranjan Singh
  • 1,567
  • 1
  • 15
  • 29
  • 4
    `var obj = {key1:[ value1,value2], key2:[value3,value4,value5]};`? – Andy Mar 13 '16 at 04:02
  • i want an object where a key belong multiple values. can you please explain if you know how to. Little more description if possible . Thank you in advance – Sujan.Prajapati Mar 13 '16 at 04:06
  • `key1:[ value1,value2]` is a key/value pair. The value is an array containing 2 values/elements. – Andy Mar 13 '16 at 04:07

1 Answers1

0

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']

dyong
  • 35
  • 1
  • 7