【发布时间】:2017-03-21 18:31:37
【问题描述】:
我的 Ember 应用程序中有 2 个数组,一个数组包含一串 ID,另一个包含 Ember 对象。
我想将 Ember 对象发送到一个函数,从存储在浏览器中的 cookie 中获取 ID 数组,然后如果 ID 属性对应于我的 IDS 数组中的字符串,则过滤掉 Ember 对象。
我试图为此使用过滤器,但每次它只带回 Ember 对象的完整列表,即使 ID 字符串数组中保存有 ID。这是我的 Ember 组件中的代码..
init () {
this._super(...arguments);
this.get('userNotificationServices').fetchActiveUserNotifications().then(result => {
this.updatedArray = this.filterArray(result);
this.set('activeNotifications', result);
});
},
filterArray(currentActiveNotifications) {
var ids;
var currentNonDimissedNotifications;
if (this.getCookie("dismissed-notifications")) {
ids = this.getCookie("dismissed-notifications").split(',');
currentNonDimissedNotifications = currentActiveNotifications.filter(function (obj) {
for (var id in ids) {
return obj.id !== id;
}
});
}
return currentNonDimissedNotifications;
}
【问题讨论】:
-
使用set方法
this.set('updatedArray', this.filterArray(result));而不是像这样直接赋值this.updatedArray = this.filterArray(result);
标签: javascript arrays ember.js