Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 使用forEach循环处理JSON_Angularjs - Fatal编程技术网

Angularjs 使用forEach循环处理JSON

Angularjs 使用forEach循环处理JSON,angularjs,Angularjs,我有一个JSON数组,我想获取具有“type”:“optional”的对象。如何在Angular JS中使用forEach实现这一点?请帮帮我 请在下面找到JSON数组 $scope.headerAll= [ { "field": "first_name", "displayName": "First name", "type": "required" }, { "field": "last_name", "displayName":

我有一个JSON数组,我想获取具有“type”:“optional”的对象。如何在Angular JS中使用forEach实现这一点?请帮帮我

请在下面找到JSON数组

 $scope.headerAll= 

 [
  {
    "field": "first_name",
    "displayName": "First name",
    "type": "required"
  },
  {
    "field": "last_name",
    "displayName": "Last Name",
    "type": "required"
  },
  {
    "field": "email",
    "displayName": "Email",
    "type": "required"
  },
  {
    "field": "isMarried",
    "displayName": "marital Status",
    "type": "optional"
  }
]

做这件事相当容易

$scope.optional = []
angular.forEach($scope.headerAll, function (v) {
  if (v.type === 'optional') {
    $scope.optional.push(v)
  }
})

这是一个更快更安全的解决方案:

 for (var i in $scope.headerAll){
    if ($scope.headerAll.hasOwnProperty(i) && $scope.headerAll[i].type === "optional"){
        console.log("do something");
    }
  }
你可以在这里检查性能


如果您想在视图本身(ngRepeat)中进行过滤,请查看此示例-非常感谢…这有助于您提供的比较中的me1.1.5有点过时此处更新了,请查看它,Igor Minar进行了更好的测试: