Javascript 在Reactjs中返回空数组的数组筛选器

Javascript 在Reactjs中返回空数组的数组筛选器,javascript,arrays,reactjs,filter,Javascript,Arrays,Reactjs,Filter,我正在尝试过滤ReactJs中的对象数组。但是,当我尝试按用户ID过滤对象时,数组返回为空 为了确保我的收藏夹道具已更新,我在componentDidMount()函数中记录了所有相关信息。console.log显示我的收藏夹道具实际上是一个包含对象的数组 componentDidUpdate() { console.log(this.props.favorites); console.log(this.props.favorites[0].userId);

我正在尝试过滤ReactJs中的对象数组。但是,当我尝试按用户ID过滤对象时,数组返回为空

为了确保我的收藏夹道具已更新,我在componentDidMount()函数中记录了所有相关信息。console.log显示我的收藏夹道具实际上是一个包含对象的数组

componentDidUpdate() {
        console.log(this.props.favorites);
        console.log(this.props.favorites[0].userId);
        console.log(this.filterFavorites());
    }

    filterFavorites() {
        return this.props.favorites.filter((favorite) => {
            return favorite.userId === this.props.currentUserId;
        });
    }

但是,我希望返回一个数组,其中包含按当前用户id筛选的对象。相反,我得到的是一个空数组,默认情况下,筛选器没有将此绑定到任何对象;它是
未定义的
,您必须在函数体之后传递此引用


您确定
favorite.userId==this.props.currentUserId
将在循环中返回true吗?可能
currentUserId
是错误的类型您是否正确地将
filterFavorites
绑定到类实例以便访问props,因此,要么你没有约束力,要么你没有你期望的价值观do@Aluan谢谢,我刚刚意识到currentUserId没有在MapStateTops中正确设置,并且返回为未定义,因此比较总是返回false。这不是真的,因为arrow函数为您进行绑定。
    filterFavorites() {
        return this.props.favorites.filter((favorite) => {
            return favorite.userId === this.props.currentUserId;
        }, this);
    }