0

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?

asked Feb 11, 2015 at 2:08

4 Answers 4

1
 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 = myData.filter(function(data){
 return data.active;
});
var inactive = myData.filter(function(data){
 return !data.active;
});

Or perhaps make it a function

function getData(type){
 return myData.filter(function(data){
 return (type == 'active') ? data.active : !data.active;
 });
}

and if you're already using ES6 arrow functions you can shorten them to:

var active = myData.filter(data => data.active);
var inactive = myData.filter(data => !data.active);
function getData(type){
 return myData.filter(data => (type == 'active') ? data.active : !data.active);
}
answered Feb 11, 2015 at 2:38
Sign up to request clarification or add additional context in comments.

1 Comment

Also if you're going to use the function then: var active = getData('active'); and var inactive = getData()
1
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 } 
];
var active = [];
var inactive = [];
for (var i in myData){
 var item = myData[i];
 if (item.active){
 active.push(item);
 }else{
 inactive.push(item);
 }
}
console.log(active, inactive);
answered Feb 11, 2015 at 2:23

Comments

0

If I'm understand you correctly, all you need to do is active.push(myData[i]) to push the reference into the array and do the same for inactive.

answered Feb 11, 2015 at 2:17

Comments

0

Instead of pushing a new object containing the active/inactive objects, just push the existing object itself.

 var active = [];
 var inactive = [];
 for (var i = 0; i < myData.length; i++) {
 if(myData[i].active) {
 active.push(myData[i]);
 } else {
 inactive.push(myData[i]);
 }
 }
answered Feb 11, 2015 at 2:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.