I want to create an array for active items and inactive items in my list. I currently have the below code. The below code works however I want the format to be the same as the existing array.
var myData = [
{"text" : "item 1", "id":11111, "active" : 0 },
{"text" : "item 2", "id":22222, "active" : 1 },
{"text" : "item 3", "id":33333, "active" : 1 },
{"text" : "item 4", "id":44444, "active" : 0 }
];
console.log(myData[0].text); //returns item 1
var active = [];
var inactive = [];
for (var i = 0; i < myData.length; i++) {
if(myData[i].active) {
active.push({
items: myData[i];
});
}
else {
inactive.push({
items: myData[i];
});
}
}
console.log(active[0].items.text); //returns item 2
console.log(inactive[0].items.text); //returns item 1
I can't seem to work out how to push the whole object into the array without naming it.
I want to setup my array so that I can console.log
active[0].text
rather than having to go to the next level and go
active[0].items.text
Is there a way I can push the whole object without naming it?