首页 > 解决方案 > 如何解决 TypeError: Cannot read property 'filter' of undefined error after replace lodash find function with native JS filter function?

问题描述

我正在尝试删除 lodash find 功能,所以我的更改基本上是替换这一行:

const current = find(items, elem => elem.id === item.id);

有了这个:

const current = items ? items.filter(function(x) {
  return x.id === item.id;
}) : null;

但我收到了这个错误:

app.js:659 Uncaught (in promise) TypeError: Cannot read property 'filter' of undefined

标签: javascript

解决方案


我不知道为什么过滤器不起作用,但这条线解决了问题

    const current         = items.find(x => x.id === item.id);

推荐阅读