简体   繁体   English

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

[英]Why is Array .filter method returning an empty array?

The below is really confusing me, I am trying to filter the musicData array to only have contents where the artist + name length are less than 25. I have logged out through the iterations etc. and everything looks good, so i'm completely stumped why it returns an empty array. 以下内容确实让我感到困惑,我试图将musicData数组过滤为仅包含艺术家+名称长度小于25的内容。我已经通过迭代等方式注销,而且一切看起来都很好,所以我很困惑为什么返回一个空数组。

Any help would be much appreciated. 任何帮助将非常感激。

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
})

You forgot the return keyword to indicate what should be returned to the new array. 您忘记了return关键字来指示应该返回到新数组的内容。 Without that, you are just executing a statement. 否则,您将只执行一条语句。

Since your logic to determine what should be in the filtered array could easily be comprised of many statements, the return keyword is needed to explicitly indicate what goes in the result. 由于确定过滤数组中应包含的内容的逻辑很容易由许多语句组成,因此需要使用return关键字来明确指示结果中包含的内容。

See documentation on Array.filter() 请参阅Array.filter()上的文档

 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); 

You are missing the return keyword to make it a statement. 您缺少return关键字来使其成为语句。

If you want to write that callback without a return then simply remove your brackets. 如果要编写该回调而没有return则只需删除括号即可。

From MDN docs : 来自MDN文档

(param1, param2, …, paramN) => { statements } (param1,param2,…,paramN)=> {语句}

(param1, param2, …, paramN) => expression (param1,param2,…,paramN)=>表达式

// equivalent to: => { return expression; //等于:=> {返回表达式; } }

Demo 演示版

 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); 

You weren't returning from the filtering function. 您不是从过滤功能中返回的。 Here you go. 干得好。

 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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM