[英]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. 我有一个对象数组,我想在某些对象中找到属性'plane:true'并将其设置为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. 我尝试使用angular forEach()函数,但没有任何反应。 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()
使用Array.prototype.forEach()
纯Javascript解决方案
The
forEach()
method executes a provided function once per array element.forEach()
方法每个数组元素执行一次提供的函数。
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' 问题是typeTransport是一个数组,'plane'不是它的属性,里面的对象有属性'plane'
rename inner forEach to value1,key1 将inner forEach重命名为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. 根据您的条件,如果为真,则设置为false,对于上述条件有效。 If you needed to make true=>false & false=>true, then below condition 如果你需要使true => false&false => true,那么就低于条件
this.typeTransport[key][key1] = !this.typeTransport[key][key1];
You have two forEach loops. 你有两个forEach循环。
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
,只需更改一下:
angular.forEach(typeTransport, function(value){
if(value.plane){
value.plane = false;
}
});
This way your model will update without having to call $scope.$apply();
这样,您的模型将更新,而无需调用$scope.$apply();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.