javascript - How to update current values inside an angular.forEach()? -
i trying convert portion of object's values integer values 1 or 0 boolean values true or false.
the structure follows:
angular.foreach(a.b.c, function (options) { ... angular.foreach(options, function (value, option) { if (value == 0) { option = false; } else { option = true; } console.log(option + " = " + value); // shows correct results; } } console.log(a.b.c) // when navigating options, options have not changed integer values.
what missing?
you changing value of local variable false/true, not changing value of object.
var array = [{ key: 1 }, { key: 0 }]; angular.foreach(array, function(options) { angular.foreach(options, function(value, option) { if (value == 0) { options[option] = false; } else { options[option] = true; } //the if else can simplified //options[option] = value != 0; console.log(option + " = " + options[option]); }) }) console.log(array);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
if know key updated then
var array = [{ key: 1 }, { key: 0 }]; angular.foreach(array, function(options) { options.key = options.key != 0; }) console.log(array);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
Comments
Post a Comment