1

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>

6 Answers 6

1

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];
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

This would probably be a good solution if OP would like to change train: true to false instead of plane: true :)
1

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

Comments

0

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

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.