Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript object duplicate keys

//It's not possible to have duplicate keys in a javascript object
const obj = {};
obj.a = 1;
obj.a = 2; //overrides the previous value of a in obj

console.log(obj.a) //prints 2

//If you want to use same key to store multiple values,
//store the values in an array inside obj
obj.key1.push("something");
obj.key1.push("something else");
console.log(obj.key1) //["something","something else"]
 
PREVIOUS NEXT
Tagged: #javascript #object #duplicate #keys
ADD COMMENT
Topic
Name
5+6 =