1

I have a javascript object which I use as type of map with key value pairs.

Population of this map occurs when value is injected to it from controller :

var people = {"2":"Brad","3":"Antonio","5":"Stacy","6":"Marry","1":"Jim"}

I want to create a new object but with one more entry i.e in pseudo code

var new_people = people + {11: "John"}

I tried to copy people to new people first and then :

new_people.\"11\" = "John"

and

new_people."11" = "John"

Both produce failure. While any string instead of number works i.e

new_people.anystring = "John"

var new_people = {"2":"Brad","3":"Antonio","5":"Stacy","6":"Marry","1":"Jim", "anystring":"John"}
Gandalf StormCrow
  • 25,788
  • 70
  • 174
  • 263
  • 1
    It's not quite true that any string will work. `1string` is a string, but `new_people.1string` won't work. – Paul D. Waite Mar 22 '13 at 13:26
  • possible duplicate of [how to access any element using javascript, if elemet is having special characters](http://stackoverflow.com/questions/12953704/how-to-access-any-element-using-javascript-if-elemet-is-having-special-characte) and [What is the most efficient way to clone a JavaScript object?](http://stackoverflow.com/q/122102/218196). – Felix Kling Mar 22 '13 at 13:32
  • 1
    In your example `new_people.anystring = "John"`, `anystring` is a sequence of characters, but it is not a string in the sense of a JavaScript data type. It is an *identifier name*. – Felix Kling Mar 22 '13 at 13:35

4 Answers4

6

You could use indexing:

new_people['11'] = 'John';
akaIDIOT
  • 9,171
  • 3
  • 27
  • 30
2

You just need to add a key value to the object:

people['11'] = "John";

If the key was a valid identifier name(not a number) you could have used this:

people.key = "John";

You have to use the indexes when it's not a valid variable name:

people["~!#$#$#HV"] ="some value";
gdoron
  • 147,333
  • 58
  • 291
  • 367
2

You will have a problem because variables hold references when storing objects. By mutating new_people, you are also mutating people.

var people = {"1":"James"}
console.log(people);       // Object {1: "James"}
var new_people = people;
new_people["2"] = "Jimmy";
console.log(people);       // Object {1: "James", 2: "Jimmy"}
console.log(new_people);   // Object {1: "James", 2: "Jimmy"}

You'll need to clone the object first then mutate the clone so you don't affect the original object.

One easy way to do that is using the underscore.js extend() method like this:

var new_people = _.extend({}, people, {"2": "Jimmy"});
console.log(people);       // Object {1: "James"}
console.log(new_people);   // Object {1: "James", 2: "Jimmy"}

Or similarly, with jQuery extend() like this:

var new_people = $.extend({}, people, {"2": "Jimmy"});
console.log(people);       // Object {1: "James"}
console.log(new_people);   // Object {1: "James", 2: "Jimmy"}
JamesOR
  • 1,138
  • 1
  • 6
  • 15
0

@Gandalf StormCrow Here is a code: jsfiddle

var people = {"2":"Brad","3":"Antonio","5":"Stacy","6":"Marry","1":"Jim"};

var newpeople = {"11": "John"};
jQuery.extend(people, newpeople);
//Now: people = {"2":"Brad","3":"Antonio","5":"Stacy","6":"Marry","1":"Jim","11": "John"}

for(var i in people){
  alert(people[i]);  
};
Khanh Tran
  • 447
  • 6
  • 21