javascript - Array.length seemingly not working; console.log shows otherwise -
i want write contactlist page though console.log showing contactlist receiving contacts being pushed localstorage, length remains @ 1! , when try iterate on contactlist write page, doesn't work expected , see undefined values should be.
var contactlist = []; window.onload = init; function init(){ var data = window.localstorage.getitem("contacts"); if(data){ mydata = json.parse(data); console.log("this local storage:\n"); console.log(mydata); console.log("this contact list before push local storage:\n"); console.log(contactlist); contactlist.push(mydata); console.log("this contact list after push local storage:\n"); console.log(contactlist); var j = contactlist.length; console.log("this length of contact list:\n"); console.log(contactlist.length); } }
here's example of console window:
this local storage:
form.js (line 12) [[[object { firstname="hi", lastname="hi", number="hi"}], object { firstname="hi", lastname="hi", number="hi"}], object{ firstname="hi", lastname="hi", number="hi"}] form.js (line 13)
this contact list before push local storage:
form.js (line 14) [] form.js (line 15)
this contact list after push local storage:
form.js (line 17) [[[[object { firstname="hi", lastname="hi", number="hi"}], object { firstname="hi", lastname="hi", number="hi"}], object { firstname="hi", > lastname="hi", number="hi"}]] form.js (line 18)
this length of contact list:
form.js (line 20) 1
that expected outcome push
. looks want use concat
.
push
append whatever argument new element @ end of array. if add string add string. if add array add array...as last element. not flatten resultant array. on other hand concat
concat 2 arrays , return new array. original array unchanged though.