javascript - how to push json data into array with 2 level or n level -
javascript - how to push json data into array with 2 level or n level -
it may silly question, right can't figure out. i'm in need.
i'm showing static info i'd create dynamic in future.
var name = []; name.push({ name: "gareth" }); name[0].push({ name: "john" }); // statement doesn't work name[0]!=[] guess. name[0].push({name:"dolly"}); i want out set like,
+name <--- expandable +0 <--- expandable name : "gareth" +0 <--- expandable name: "john" +1 <--- expandable name: "dolly" i know not hard one. i'm unable figure out right now. help appreciated.
you can't push onto name[0] not array can still assign elements number's , create own push.
var makepushable = function (obj) { obj.push = function (item) { this[this.length] = item; this.length++; }; obj.length = 0; //keep track of how many elements } var name = []; name.push({ name: "gareth" }); makepushable(name[0]); name[0].push({ name: "john" }); name[0].push({ name:"dolly" }); and get:
name[0] ---> { name:"gareth", 0: {name:"john"}, 1: {name:"dolly"}, length: 2, push: function () { ... } } name[0][0] ---> {name:"john"} name[0][1] ---> {name:"dolly"} if don't want push , length show on for ( prop in xxx ) if (xxx.hasownproperty(prop)), create class so:
var pushable = function (obj) { (var in obj) { this[i] = obj[i]; } object.setprototypeof(this, { push: function (item) { this[this.length] = item; object.getprototypeof(this).length++; }, length: 0 }); }; pushable.prototype.push = function (item) { this[this.length] = item; object.getprototypeof(this).length++; }; pushable.prototype.length = 0; var name = []; name.push(new pushable({ name:"gareth" })); name[0].push(new pushable({ name: "john" })); name[0].push(new pushable({ name:"dolly" })); name[0][1].push(new pushable({ name:"last kid" })); you create push function, automatically create pushable class if wanted.
javascript json angularjs
Comments
Post a Comment