37

I have an array as :

items=[{'id':1},{'id':2},{'id':3},{'id':4}];

How should I add a new pair {'id':5} to the array?

4
  • Thanks @NDM for comment. I tried some code on jsfiddle and it is working (with items.push({'id':5})....) But when I use the same code, the browser shows {{item.id}} instead of showing the updated value of items (I am using angularjs btw). I don't know why is this happening. Commented Sep 23, 2013 at 8:33
  • possible duplicate of Add new value to an existing array in JavaScript Commented Sep 23, 2013 at 16:12
  • The OP is creating an array of objects, not an array of values. Commented Sep 23, 2013 at 16:23
  • Possible duplicate of Appending to array Commented Dec 15, 2015 at 15:32

5 Answers 5

64

Use .push:

items.push({'id':5});
Sign up to request clarification or add additional context in comments.

3 Comments

how can i add it at particular position in the array? say add it at index 3
@separ1 arr[2] = "name";
@Shmulik That will replace position 2 rather than add it there. You actually want arr.splice(3, 0, {'id': 3})
12

.push() will add elements to the end of an array.

Use .unshift() if need to add some element to the beginning of array i.e:

items.unshift({'id':5});

Demo:

items = [{'id': 1}, {'id': 2}, {'id': 3}, {'id': 4}];
items.unshift({'id': 0});
console.log(items);

And use .splice() in case you want to add object at a particular index i.e:

items.splice(2, 0, {'id':5});
           // ^ Given object will be placed at index 2...

Demo:

items = [{'id': 1}, {'id': 2}, {'id': 3}, {'id': 4}];
items.splice(2, 0, {'id': 2.5});
console.log(items);

3 Comments

May I know how to add [{'id': 1,'newId': 'x' }, {'id': 2,'newId': 'y'}, {'id': 3,'newId': 'z'}] ?
@Codenewbie Sorry, I didn't get your question. It would be great if you please create some working example with expected sample data and expected output.
sure , i would ask
7

Sometimes .concat() is better than .push() since .concat() returns the new array whereas .push() returns the length of the array.

Therefore, if you are setting a variable equal to the result, use .concat().

items = [{'id': 1}, {'id': 2}, {'id': 3}, {'id': 4}];
newArray = items.push({'id':5})

In this case, newArray will return 5 (the length of the array).

newArray = items.concat({'id': 5})

However, here newArray will return [{'id': 1}, {'id': 2}, {'id': 3}, {'id': 4}, {'id': 5}].

Comments

2

If you're doing jQuery, and you've got a serializeArray thing going on concerning your form data, such as :

var postData = $('#yourform').serializeArray();

// postData (array with objects) : 
// [{name: "firstname", value: "John"}, {name: "lastname", value: "Doe"}, etc]

...and you need to add a key/value to this array with the same structure, for instance when posting to a PHP ajax request then this :

postData.push({"name": "phone", "value": "1234-123456"});

Result:

// postData : 
// [{name: "firstname", value: "John"}, {name: "lastname", value: "Doe"}, {"name":"phone","value":"1234-123456"}]

Comments

2

New solution with ES6

Default object

object = [{'id': 1}, {'id': 2}, {'id': 3}, {'id': 4}];

Another object

object =  {'id': 5};

Object assign ES6

resultObject = {...obj, ...newobj};

Result

[{'id': 1}, {'id': 2}, {'id': 3}, {'id': 4}, {'id': 5}];

2 Comments

Please make your answer more elaborate. Explain how it works.
I think it should be ary1 = [{'id': 1}, {'id': 2}];ary2 = [{'id': 3}];join = [...ary1,...ary2];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.