简体   繁体   中英

How to find and edit property of object in the array of an objects?

I have an array of objects, and I want to find property 'plane: true' in some objects and set it to false. And it just adds to array on the same area as an objects. I tried to use function of angular forEach() but nothing happened. Help please.

 this.typeTransport = [ { currentTransport: 'plane', changeTransport: 'plane_disable', plane: true }, { currentTransport: 'train', changeTransport: 'train_disable', train: true }, { currentTransport: 'bus', changeTransport: 'bus_disable', bus: true }, { currentTransport: 'ship', changeTransport: 'ship_disable', ship: true } ]; angular.forEach(this.typeTransport, function(value, key){ angular.forEach(value, function(value, key){ if(key === 'plane'){ this.typeTransport[key] == false ? this.typeTransport[key] = true : this.typeTransport[key] = false; console.log(this.typeTransport); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 

A solution in plain Javascript with Array.prototype.forEach()

The forEach() method executes a provided function once per array element.

 var typeTransport = [{ currentTransport: 'plane', changeTransport: 'plane_disable', plane: true }, { currentTransport: 'train', changeTransport: 'train_disable', train: true }, { currentTransport: 'bus', changeTransport: 'bus_disable', bus: true }, { currentTransport: 'ship', changeTransport: 'ship_disable', ship: true }]; typeTransport.forEach(function (a) { if (a.plane) { a.plane = false; } }) document.write('<pre>' + JSON.stringify(typeTransport, 0, 4) + '</pre>'); 

The problem is that typeTransport is an array and 'plane' is not a property of it, the object inside has the property 'plane'

rename inner forEach to value1,key1

 angular.forEach(this.typeTransport, function(value, key) {
 angular.forEach(value, function(value1, key1) {

 if (key1 === 'plane') {
      this.typeTransport[key][key1] = false;
    }
  });
 });

As per your condition, you are setting false if true, for which above condition works. If you needed to make true=>false & false=>true, then below condition

 this.typeTransport[key][key1] = !this.typeTransport[key][key1];

You have two forEach loops.

In the second loop you have to refer to each single item instead the array

angular.forEach(this.typeTransport, function(item){
          angular.forEach(item, function(value, key){
            if(key === 'plane'){
    item[key] == false ? item[key] = true : item[key] = false;
             console.log(item); 
             console.log(this.typeTransport); 
            }
          });
      });

I'm not sure, that understand your question but this should work.

this.tranportType.forEach(function(tt){
     if(tt.hasOwnProperty('plane') && tt.plane){
        tt.plane = false; 
     }
})

Just to add some 'old school' :)

for (var i = 0; i < this.typeTransport.length; i++) {
  if (this.typeTransport[i].plane == true) {
    this.typeTransport[i].plane = false;
  }
}

You can still use angular.forEach , just change it a little bit:

angular.forEach(typeTransport, function(value){
    if(value.plane){
        value.plane = false;
    }
});

This way your model will update without having to call $scope.$apply();

Here's a fiddle .

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