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:
- is there way
concat
arrays override index 0 on initialconcat
? - or there way use
concat
without declaringa = [{}]
?
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
Post a Comment