寻找过滤数组,并根据标志将它们分为2个数组,如果为true或false

时间:2019-01-28 02:59:04

标签: javascript angularjs

我计划根据一个内部数组中的标记将一个数组过滤为2个单独的数组,但遇到了麻烦。请帮助我提供我的代码。 我们如何从apiData中获得2个单独的数组,以根据标记值将对象过滤为类型数组

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

3 个答案:

答案 0 :(得分:1)

我编写了一个简单的过滤器功能。请看看!

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

答案 1 :(得分:0)

一种解决方案是将map()filter()结合使用以获得新的types数组。

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

答案 2 :(得分:0)

这里是一个示例代码,该示例创建一个具有布尔值的对象,并根据其布尔值创建2个对象数组。抱歉,如果我误解了您的期望。

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

编辑:我清理了它,以在创建对象时自动将它们添加到数组中。