12

I have an array of objects that looks like this:

event_id=[{"0":"e1"},{"0","e2"},{"0","e4"}];

How do I add an element to that array?

I thought of

event_id.splice(1,0,{"0":"e5"});

Thanks.

Naftali
  • 144,921
  • 39
  • 244
  • 303
user823527
  • 3,644
  • 17
  • 66
  • 110

5 Answers5

12

If you just want to add a value to the end of an array then the push(newObj) function is easiest, although splice(...) will also work (just a bit trickier).

var event_id = [{"0":"e1"}, {"0":"e2"}, {"0":"e4"}];
event_id.push({"0":"e5"});
//event_id.splice(event_id.length, 0, {"0":"e5"}); // Same as above.
//event_id[event_id.length] = {"0":"e5"}; // Also the same.
event_id; // => [{"0":"e1"}, {"0":"e2"}, {"0":"e4"}, {"0":"e5"}]; 

See the excellent MDN documentation for the Array object for a good reference of the methods and properties available on arrays.

[Edit] To insert something into the middle of the array then you'll definitely want to use the splice(index, numToDelete, el1, el2, ..., eln) method which handles both deleting and inserting arbitrary elements at any position:

var a  = ['a', 'b', 'e'];
a.splice( 2,   // At index 2 (where the 'e' is),
          0,   // delete zero elements,
         'c',  // and insert the element 'c',
         'd'); // and the element 'd'.
a; // => ['a', 'b', 'c', 'd', 'e']
maerics
  • 151,642
  • 46
  • 269
  • 291
11

Since I want to add the object in the middle of the array, I ended with this solution:

var add_object = {"0": "e5"};
event_id.splice(n, 0, add_object); // n is declared and is the index where to add the object
user823527
  • 3,644
  • 17
  • 66
  • 110
3

ES6 solution with spread operator:

event_id=[{"0":"e1"},{"0","e2"},{"0","e4"}];
event_id = [...event_id,{"0":"e5"}]

or if you do not want to mutate event_id

newEventId = [...event_id,{"0":"e5"}]

UPDATE: to insert object after a specific index or object key or object value respectively you can:

const arr = [{a:1},{b:2},{c:3},{d:4}]

arr.reduce((list,obj,index)=>index===1 ? [...list,obj,{g:10}] : [...list,obj], [])
arr.reduce((list,obj)=>Object.keys(obj)[0]==='b' ? [...list,obj,{g:10}] : [...list,obj], [])
arr.reduce((list,obj)=>Object.values(obj)[0]===2 ? [...list,obj,{g:10}] : [...list,obj], [])

// output:  [ { a: 1 }, { b: 2 }, { g: 10 }, { c: 3 }, { d: 4 } ]
AndreasT
  • 921
  • 6
  • 12
  • You can't add a object with this method in between of the array of objects. Only start and end will work right – RBG Feb 21 '20 at 10:03
  • yes you are right...I just updated the answer for adding object in between of the array – AndreasT Feb 22 '20 at 13:01
0
event_id.push({"something", "else"});

Try using .push(...) ^

Naftali
  • 144,921
  • 39
  • 244
  • 303
0

Well you could typically use:

event_id[event_id.length] = {"0":"e5"};

or (the slightly slower)

event_id.push({"0":"e5"});

though if you mean to insert an element into the middle of an array and not always on the end, then we'll have to come up with something a bit more creative.

Hope it helps,

ise

ise
  • 228
  • 2
  • 8