Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.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
Javascript 如何在所有对象中插入属性?_Javascript_Angularjs_Angularjs Directive_Angularjs Scope_Angularjs Ng Repeat - Fatal编程技术网

Javascript 如何在所有对象中插入属性?

Javascript 如何在所有对象中插入属性?,javascript,angularjs,angularjs-directive,angularjs-scope,angularjs-ng-repeat,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,Angularjs Ng Repeat,我正在尝试在数组中存在的所有对象中添加属性。我将解释更多内容,换句话说,我有一个对象数组。我需要在所有对象中添加“selected”属性。前两个对象的“selected”属性值为true,其余元素的值为false。我需要添加“selected”:前两个元素为true,其余元素为“selected”:false 其次,我们可以从数组中删除那些属性为“隐藏”的对象吗 angular.module('app', ['ionic']).controller('firstCntrl',function($

我正在尝试在数组中存在的所有对象中添加属性。我将解释更多内容,换句话说,我有一个对象数组。我需要在所有对象中添加“selected”属性。前两个对象的“selected”属性值为true,其余元素的值为false。我需要添加“selected”:前两个元素为true,其余元素为“selected”:false

其次,我们可以从数组中删除那些属性为“隐藏”的对象吗

angular.module('app', ['ionic']).controller('firstCntrl',function($scope){


    $scope.copyArrary =[];
    angular.forEach($scope.data, function(value,key) {
      console.log(value + key)
    }); 
})
js小提琴

要删除,我将首先过滤:

$scope.data = $scope.data.filter(function (element) {
   return !element.hidden;
}
然后设置属性:

for(var i = 0; i < $scope.data.length; ++i) {
   $scope.data[i]['selected'] = i <= 1;
}
for(变量i=0;i<$scope.data.length;++i){

$scope.data[i]['selected']=i要删除,我将首先筛选:

$scope.data = $scope.data.filter(function (element) {
   return !element.hidden;
}
然后设置属性:

for(var i = 0; i < $scope.data.length; ++i) {
   $scope.data[i]['selected'] = i <= 1;
}
for(变量i=0;i<$scope.data.length;++i){

$scope.data[i]['selected']=i要删除,我将首先筛选:

$scope.data = $scope.data.filter(function (element) {
   return !element.hidden;
}
然后设置属性:

for(var i = 0; i < $scope.data.length; ++i) {
   $scope.data[i]['selected'] = i <= 1;
}
for(变量i=0;i<$scope.data.length;++i){

$scope.data[i]['selected']=i要删除,我将首先筛选:

$scope.data = $scope.data.filter(function (element) {
   return !element.hidden;
}
然后设置属性:

for(var i = 0; i < $scope.data.length; ++i) {
   $scope.data[i]['selected'] = i <= 1;
}
for(变量i=0;i<$scope.data.length;++i){

$scope.data[i]['selected']=i您尝试过什么?您知道本机数组#map()?下划线#filter()?您可以使用CodePen您尝试过什么?您知道本机数组#map()?下划线#filter()?您可以使用CodePen您尝试过什么?您知道本机数组#map()?下划线#filter()吗你能用codepen吗你试过什么?你知道本机数组#map()?下划线#filter()?你能用codepen吗