简体   繁体   English

如何使用Ember.js Array forEach更改值?

[英]how to change value with Ember.js Array forEach?

 self.resultList.forEach(function(item, index, enumerable){
                         console.log(self.resultList);
                         item.id=11;
                         item.get('id');
                     });

the item like this: 像这样的项目: 在此输入图像描述

if item.id = 11; 如果item.id = 11; the exception like this: 像这样的例外:

Assertion failed: You must use Ember.set() to access this property (of [object Object]) 断言失败:您必须使用Ember.set()来访问此属性([object Object])

so item.get('id') or item.set('id',11) 所以item.get('id')或item.set('id',11)

the exception like this 像这样的例外

Uncaught TypeError: Object # has no method 'get' 未捕获的TypeError:对象#没有方法'get'

is this item not the Ember's Object?so what the item is? 这个项目不是Ember的对象吗?那么这个项目是什么?

could someone tell me how to change the 'itme.id's value.. 有人能告诉我如何改变'itme.id的价值..

Thanks a million 太感谢了

You can use the Ember.set(yourObject, propertyName, value); 您可以使用Ember.set(yourObject, propertyName, value); and Ember.get(yourObject, propertyName); Ember.get(yourObject, propertyName); to safely set and get properties. 安全地设置和获取属性。

In your case: 在你的情况下:

self.resultList.forEach(function(item, index, enumerable) {
    Ember.set(item, "id", 11); 
    Ember.get(item, "id");
});

In my case I did it in this way 就我而言,我是这样做的

 //In my controller I've defined the array displayInfoCheckboxes: [ { turnover: { label: "Turnover", value: 1, propertyName: "turnover" } }, { pl: { label: "P&L", value: 1 } } ] //and in my handler I passed the full string path to the property in the set method let displayInfoCheckboxes = this.get('displayInfoCheckboxes'); let controller = this; displayInfoCheckboxes.forEach(function(items,index) { for (var key in items) { controller.set('displayInfoCheckboxes.' + index + '.' + key + '.value', false); } }) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM