简体   繁体   中英

Looking to filter array and make them into 2 arrays based on a flag if true or false

I am planning to filter an array into 2 separate arrays based on flag in one of the inner arrays but having trouble. Please help me with my code. How do we get 2 separate arrays out of apiData to have objects filtered in types array based on flag value

 var apiData = [{ "id": 1, "types": [{ "id": "1.1", "flag": true, }, "id": "1.2", "flag": false }] }, "id": 2, "types": [{ "id": "2.1", "flag": true, }] } ] My Result should be like this for filteredTrueArray [{ "id": 1, "types": [{ "id": "1.1", "flag": true, }] }, "id": 2, "types": [{ "id": "2.1", "flag": true, }] } ] I wanted $scope.filteredTrueArray to have types array with flag=true value objects and another array to have types array with only flag=false objects. Below is my code $scope.filteredTrueArray = apiData.filter(function(item) { var isTrueFound = item.types.some(function (el) { return el.flag == true; }); if(isTrueFound){ for(var i=0;i<item.types.length>0;i++) { if(item.types[i].flag == true){ $scope.filteredTrueArray.push(item.types[i]); } } } });

I've written a simple filter function. Please take a look!

var apiData = [{
  "id": 1,
  "types": [{
    "id": "1.1",
    "flag": true,
  }, {
    "id": "1.2",
    "flag": false
  }]
}, {

  "id": 2,
  "types": [{
    "id": "2.1",
    "flag": true,
  }]
}];

function filterByTypeFlag(records, flagValue) {
  var filtered = [];

  records.forEach(function (record) {
    var matchedTypes = [];

    record.types.forEach(function (type) {
      if (type.flag === flagValue) {
        matchedTypes.push(type);
      }
    });

    if (matchedTypes.length) {
      filtered.push({
        "id": record.id,
        "types": matchedTypes
      });
    }
  });
  return filtered;
}

filterByTypeFlag(apiData, true);
filterByTypeFlag(apiData, false);

One solution is to use map() with a filter() for get the new types array.

 var apiData = [ { "id": 1, "types": [ {"id": "1.1", "flag": true}, {"id": "1.2", "flag": false} ] }, { "id": 2, "types": [ {"id": "2.1", "flag": true} ] } ]; let filteredTrueArray = apiData.map( ({id, types}) => ({id, types: types.filter(x => x.flag)}) ) .filter(({types}) => types.length); let filteredFalseArray = apiData.map( ({id, types}) => ({id, types: types.filter(x => !x.flag)}) ) .filter(({types}) => types.length); console.log("FilteredTrueArray:", filteredTrueArray); console.log("FilteredFalseArray:", filteredFalseArray);

Here is a sample code that creates an object with a boolean value and creates 2 arrays of objects bases off their boolean value. Sorry if I misunderstood what you were looking for.

var objArray = [];
class testObj {
  constructor(Oname, test1) {
    this.name = Oname;
    this.isABoolean = test1;
    objArray.push(this);
  }
}

var test1 = new testObj("test1", false);
var test2 = new testObj("test2", true);
var test3 = new testObj("test3", false);
var test4 = new testObj("test4", true);
var test5 = new testObj("test5", false);
var objArray = [test1, test2, test3, test4, test5];
var trueArray = [];
var falseArray = [];

function createArrays() {
  for (var i = 0; i < objArray.length; i++) {
    if (objArray[i].isABoolean === true) {
      trueArray.push(objArray[i]);
      //console.log(trueArray[i].name);
    } else if (objArray[i].isABoolean === false) {
      falseArray.push(objArray[i]);
      }
    }
  }
  createArrays();
  for (var j = 0; j < trueArray.length; j++) {
    console.log("True value: " + trueArray[j].name);
  }
  for (var k = 0; k < falseArray.length; k++) {
    console.log("False value " + falseArray[k].name);
  }

EDIT: I cleaned it up to automatically add the objects to an array upon creation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM