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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17976333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:18:06  来源:igfitidea点击:

how to change value with Ember.js Array forEach?

javascriptember.js

提问by seagod

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

the item like this: enter image description here

像这样的项目: 在此处输入图片说明

if item.id = 11;the exception like this:

如果item.id = 11;异常是这样的:

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'

未捕获的类型错误:对象 # 没有方法“get”

is this item not the Ember's Object?so what the item is?

这件物品不是 Ember's Object 吗?那件物品是什么?

could someone tell me how to change the 'itme.id's value..

有人可以告诉我如何更改“itme.id”的值..

Thanks a million

太感谢了

回答by Marcio Junior

You can use the Ember.set(yourObject, propertyName, value);and Ember.get(yourObject, propertyName);to safely set and get properties.

您可以使用Ember.set(yourObject, propertyName, value);Ember.get(yourObject, propertyName);来安全地设置和获取属性。

In your case:

在你的情况下:

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

回答by bedrosenator

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