我试图创建一个可以通过“闹钟”过滤掉我的对象的计算函数,
所以我创建了一个名为filteredAlarms的计算函数,在我的循环中我做了一个v-for循环:
<li class="event-log__item timeline__item" v-for="(item, key) in filteredAlarms" :key="key">
在我的过滤器中尝试执行以下操作:
let filteredAlarms = Object.keys(this.eventLog).forEach(key => {
this.eventLog[key].filter(item => {
return item.type.indexOf("alarm") > -1
});
});
return filteredAlarms
不幸的是,这不起作用 - 我得到一个错误:TypeError:this.eventLog.filter不是函数
我做错了什么? :)
我试图过滤的对象类似于下面的对象:
"system_events": {
"1013": [{
"id": 25899,
"timestamp": "2017-08-15T21:26:42Z",
"type": "alarm",
"code": 190,
"title": "",
"description": "",
"appeared": "2017-08-15T21:26:40Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Randers pr 44b sidste station"
},
{
"id": 26157,
"timestamp": "2017-08-15T21:32:17Z",
"type": "warning",
"code": 190,
"title": "",
"description": "",
"appeared": "2017-08-15T21:32:06Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Randers pr 44b sidste station"
},
{
"id": 26387,
"timestamp": "2017-08-15T21:37:38Z",
"type": "info",
"code": 190,
"title": "",
"description": "",
"appeared": "2017-08-15T21:37:33Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Randers pr 44b sidste station"
}],
"1015": [{
"id": 23777,
"timestamp": "2017-08-15T20:38:08Z",
"type": "alarm",
"code": 191,
"title": "",
"description": "",
"appeared": "2017-08-15T20:38:00Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Favrskov Svenstrup gyvelvej"
},
{
"id": 23779,
"timestamp": "2017-08-15T20:38:08Z",
"type": "alarm",
"code": 190,
"title": "",
"description": "",
"appeared": "2017-08-15T20:37:58Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Favrskov Svenstrup gyvelvej"
},
{
"id": 23841,
"timestamp": "2017-08-15T20:39:41Z",
"type": "alarm",
"code": 192,
"title": "",
"description": "",
"appeared": "2017-08-15T20:39:31Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Favrskov Svenstrup gyvelvej"
},
{
"id": 25243,
"timestamp": "2017-08-15T21:12:03Z",
"type": "alarm",
"code": 191,
"title": "",
"description": "",
"appeared": "2017-08-15T21:11:55Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Favrskov Svenstrup gyvelvej"
},
{
"id": 25529,
"timestamp": "2017-08-15T21:18:11Z",
"type": "alarm",
"code": 190,
"title": "",
"description": "",
"appeared": "2017-08-15T21:18:00Z",
"disappeared": null,
"acknowlegded": null,
"solved": null,
"system_name": "Favrskov Svenstrup gyvelvej"
}]
}
答案 0 :(得分:0)
使用forEach,你不会过滤任何东西,你会迭代。并且内部过滤器返回新数组,因此它也不会更改eventLog[key]
。
尝试这样的事情:
let filteredAlarams = {}
Object.keys(this.eventLog).forEach(key => {
filteredAlarams[key] = this.eventLog[key].filter(item => {
return item.type.indexOf("alarm") > -1
});
});
return filteredAlarms
答案 1 :(得分:0)
首先,filteredAlarms
的值为undefined
,因为forEach
不会返回任何内容。
其次,您应该知道filter()
返回一个新数组。您没有在任何地方使用新的过滤数组。
看起来像这样
const filteredAlarms = Object.keys(this.eventLog).reduce((result, key) => {
result[key] = this.eventLog[key].filter(item => {
return item.type.indexOf("alarm") > -1
})
return result
}, {})
return filteredAlarms
然后是
TypeError: this.eventLog.filter is not a function
我猜测你是在尝试迭代上面发布的整个对象,但是你应该迭代data.system_events
属性。