为什么Array .filter方法返回一个空数组?

时间:2018-04-20 15:58:23

标签: javascript arrays

下面真的让我感到困惑,我试图将musicData数组过滤到只有艺术家+名字长度小于25的内容。我已经通过迭代等注销,一切看起来都不错,所以我' m完全难以理解为什么它返回一个空数组。

非常感谢任何帮助。

const musicData = [
    { artist: 'Adele', name: '25', sales: 1731000 },
    { artist: 'Drake', name: 'Views', sales: 1608000 },
    { artist: 'Beyonce', name: 'Lemonade', sales: 1554000 },
    { artist: 'Chris Stapleton', name: 'Traveller', sales: 1085000 },
    { artist: 'Pentatonix', name: 'A Pentatonix Christmas', sales: 904000 },
    { artist: 'Original Broadway Cast Recording', name: 'Hamilton: An American Musical', sales: 820000 },
    { artist: 'Twenty One Pilots', name: 'Blurryface', sales: 738000 },
    { artist: 'Prince', name: 'The Very Best of Prince', sales: 668000 },
    { artist: 'Rihanna', name: 'Anti', sales: 603000 },
    { artist: 'Justin Bieber', name: 'Purpose', sales: 554000 }
];

var totalAlbumSales=musicData.filter((curr) => {
    curr.artist.length+curr.name.length <25
})

3 个答案:

答案 0 :(得分:4)

您忘记了return关键字以指示应该将哪些内容返回到新数组。没有它,你只是在执行一个声明。

由于您确定过滤数组中应该包含哪些内容的逻辑可以很容易地由许多语句组成,因此需要return关键字来明确指出结果中的内容。

请参阅 Array.filter()

上的文档

&#13;
&#13;
const musicData = [
    { artist: 'Adele', name: '25', sales: 1731000 },
    { artist: 'Drake', name: 'Views', sales: 1608000 },
    { artist: 'Beyonce', name: 'Lemonade', sales: 1554000 },
    { artist: 'Chris Stapleton', name: 'Traveller', sales: 1085000 },
    { artist: 'Pentatonix', name: 'A Pentatonix Christmas', sales: 904000 },
    { artist: 'Original Broadway Cast Recording', name: 'Hamilton: An American Musical', sales: 820000 },
    { artist: 'Twenty One Pilots', name: 'Blurryface', sales: 738000 },
    { artist: 'Prince', name: 'The Very Best of Prince', sales: 668000 },
    { artist: 'Rihanna', name: 'Anti', sales: 603000 },
    { artist: 'Justin Bieber', name: 'Purpose', sales: 554000 }
];

var totalAlbumSales=musicData.filter((curr) => {
    return curr.artist.length + curr.name.length < 25
})

console.log(totalAlbumSales);
&#13;
&#13;
&#13;

答案 1 :(得分:3)

您缺少return关键字以使其成为声明。

如果你想在没有return的情况下编写回调,那么只需删除括号。

来自MDN docs

  

(param1,param2,...,paramN)=&gt; {statements}

     

(param1,param2,...,paramN)=&gt;表达

     

//相当于:=&gt; {return expression; }

<强>演示

&#13;
&#13;
const musicData = [
    { artist: 'Adele', name: '25', sales: 1731000 },
    { artist: 'Drake', name: 'Views', sales: 1608000 },
    { artist: 'Beyonce', name: 'Lemonade', sales: 1554000 },
    { artist: 'Chris Stapleton', name: 'Traveller', sales: 1085000 },
    { artist: 'Pentatonix', name: 'A Pentatonix Christmas', sales: 904000 },
    { artist: 'Original Broadway Cast Recording', name: 'Hamilton: An American Musical', sales: 820000 },
    { artist: 'Twenty One Pilots', name: 'Blurryface', sales: 738000 },
    { artist: 'Prince', name: 'The Very Best of Prince', sales: 668000 },
    { artist: 'Rihanna', name: 'Anti', sales: 603000 },
    { artist: 'Justin Bieber', name: 'Purpose', sales: 554000 }
];

var totalAlbumSales=musicData.filter((curr) => curr.artist.length + curr.name.length < 25);

console.log(totalAlbumSales);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您没有从过滤功能返回。你走了。

const musicData = [
    { artist: 'Adele', name: '25', sales: 1731000 },
    { artist: 'Drake', name: 'Views', sales: 1608000 },
    { artist: 'Beyonce', name: 'Lemonade', sales: 1554000 },
    { artist: 'Chris Stapleton', name: 'Traveller', sales: 1085000 },
    { artist: 'Pentatonix', name: 'A Pentatonix Christmas', sales: 904000 },
    { artist: 'Original Broadway Cast Recording', name: 'Hamilton: An American Musical', sales: 820000 },
    { artist: 'Twenty One Pilots', name: 'Blurryface', sales: 738000 },
    { artist: 'Prince', name: 'The Very Best of Prince', sales: 668000 },
    { artist: 'Rihanna', name: 'Anti', sales: 603000 },
    { artist: 'Justin Bieber', name: 'Purpose', sales: 554000 }
];

var totalAlbumSales=musicData.filter((curr) => {
    return curr.artist.length+curr.name.length <25
})

console.log(totalAlbumSales)