[英]filter not working with anonymous function
I'm trying to learn react so I know this isn't the best way to make a copy but why doesn't the following work: 我正在尝试学习反应,所以我知道这不是制作副本的最佳方法,但为何以下工作不起作用:
var allItems = this.state.items;
allItems =allItems.filter(function(e){return e;});
I keep getting filter is not a function 我不断收到过滤器不是功能
Since this.state.items
is an Object, not an Array, you will need to loop over the keys like so: 由于this.state.items
是一个对象,而不是数组,因此您需要像这样循环遍历键:
var filteredArray = [];
Object.keys(allItems).forEach(function(key) {
// do something with each item
console.log(key + ': ' + allItems[key]);
// like add it to a filtered array, conditionally if you want to:
filteredArray.push(allItems[key]);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.