ak85
ak85

Reputation: 4264

push existing array into multiple arrays js

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?

Upvotes: 0

Views: 122

Answers (4)

Edwin Reynoso
Edwin Reynoso

Reputation: 1531

 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);
}

Upvotes: 1

taesu
taesu

Reputation: 4580

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);

Upvotes: 1

tonesforchris
tonesforchris

Reputation: 303

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]);
          }
  }

Upvotes: 0

jungy
jungy

Reputation: 3087

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.

Upvotes: 0

Related Questions