javascript - Concat JSON array objects without index 0 undefined -


this basic question i'm curious elegant way (i have solution dirty way). have code below start empty var a json array. want concat more json objects array later on:

var = [{}]; = a.concat([{"a":"b1"},{"a":"b2"}]); a.splice(0,1); // not work remove index 0 of undefined console.log(a[0].a + " " + a[1].a + " " + a[2].a); 

the problem when a declared, came undefined in index 0. re-generate whole array remove undefined using push.apply() seems unnecessary overhead. see:

javascript: how remove array item(json object) based on item property value?

two questions:

  1. is there way concat arrays override index 0 on initial concat?
  2. or there way use concat without declaring a = [{}]?

you should declaring empty array, there no need empty object inside.

var = []; 

output in console

> var = [];   undefined > = a.concat([{"a":"b1"},{"a":"b2"}]);   [object, object] 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -