【发布时间】:2019-08-22 19:54:10
【问题描述】:
这是我的对象(确保它是 typeof 对象):
{
"1": {"user_id":1,"test":"","user_name":"potato0","isok":"true"},
"2":{"user_id":2,"test":"","user_name":"potato1","isok":" true"},
"3":{"user_id":3,"test":"","user_name":"potato2","isok":" true"},
"4":{"user_id":4,"test":"","user_name":"potato3","isok":"locationd"}
}
为什么使用.filter 对我不起作用?
是不是因为我的变量是typeof object 并且该方法仅适用于数组?
this.activeUsers = window.users.filter( function(user) {
// return ( (user.test === '0') && (user.isok === '0') );
return user.user_id === 1;
});
得到错误:
.filter 不是函数
建议的对象替代方案是什么?
【问题讨论】:
-
filter适用于数组。你的不是数组。如有疑问,请始终参考docs。 -
过滤对象的建议解决方案是什么? @31piy
-
@wellhellothere 修复任何给你一个对象而不是数组的东西。
-
我认为这是一个很好的问题。只需添加 Array.prototype.filter 仅适用于数组的注释,因此您必须使用一些 Object.values() 或 Object.keys() 来使用..
-
@KevinB 你能帮我提出你的建议吗? stackoverflow.com/questions/55512730/…
标签: javascript ecmascript-6 filter functional-programming