开发者问题收集

如何返回 reduce() 中累加器的长度

2018-07-30
786

我正在学习 JavaScript 中的 Reduce 迭代器,我试图检查累加器的长度,但它一直返回 TypeError:无法读取未定义的属性“length”

这是我正在使用的数组

var songs = [
{
    name: 'Smooth',
    artist: 'Santana featuring Rob Thomas',
    year: 1999,
    month: 10,
    day: 23,
    weeksAtNumberOne: 12,
    duration: '4:00',
},
{
    name: 'What a Girl Wants',
    artist: 'Christina Aguilera',
    year: 2000,
    month: 6,
    day: 15,
    weeksAtNumberOne: 2,
    duration: '3:20',
},
{
    name: 'I Knew I Loved You',
    artist: 'Savage Garden',
    year: 2000,
    month: 8,
    day: 29,
    weeksAtNumberOne: 4,
    duration: '4:10',
},
{
    name: 'Thank God I Found You',
    artist: 'Mariah Carey featuring Joe and 98 Degrees',
    year: 2000,
    month: 2,
    day: 19,
    weeksAtNumberOne: 2,
    duration: '4:17',
},
{
    name: 'Amazed',
    artist: 'Lonestar',
    year: 2000,
    month: 7,
    day: 4,
    weeksAtNumberOne: 2,
    duration: '4:00',
}]

代码

function summerCount(arr) {
return arr.reduce(function(init, next) {
    if ([6,7,8].includes(next.month)) {
        init.push(next.month);
    };
     return init.length;
}, [])};

如果我只返回 init ,它会返回数组,但如果我添加 .length 属性,它会抛出 TypeError。 提前谢谢您

2个回答

您应该仅在所有迭代完成后检查累加器的长度,而不是在此之前,否则下一次迭代中的累加器的值将是一个数字,而不是一个数组(并且您的代码依赖于累加器始终是一个 数组 ):

var songs=[{name:'Smooth',artist:'Santana featuring Rob Thomas',year:1999,month:10,day:23,weeksAtNumberOne:12,duration:'4:00',},{name:'What a Girl Wants',artist:'Christina Aguilera',year:2000,month:6,day:15,weeksAtNumberOne:2,duration:'3:20',},{name:'I Knew I Loved You',artist:'Savage Garden',year:2000,month:8,day:29,weeksAtNumberOne:4,duration:'4:10',},{name:'Thank God I Found You',artist:'Mariah Carey featuring Joe and 98 Degrees',year:2000,month:2,day:19,weeksAtNumberOne:2,duration:'4:17',},{name:'Amazed',artist:'Lonestar',year:2000,month:7,day:4,weeksAtNumberOne:2,duration:'4:00',}]

function summerCount(arr) {
  return arr.reduce(function(init, next) {
    if ([6, 7, 8].includes(next.month)) {
      init.push(next.month);
    };
    return init;
  }, []).length;
}
console.log(summerCount(songs));
CertainPerformance
2018-07-30

当您在 reduce()return init.length 时,它将成为下一个累加器。因此,下次通过 reduce 时,您会尝试在整数上查找 length 属性,但这不起作用(正如您所注意到的)。

如果您想要长度,只需使用 reduce 返回数组( return init )并查看返回数组的长度。

如果您 只是 想要计数,则在遍历时将其添加到累加器并返回数字:

var songs = [{name: 'Smooth',artist: 'Santana featuring Rob Thomas',year: 1999,month: 10,day: 23,weeksAtNumberOne: 12,duration: '4:00',},{name: 'What a Girl Wants',artist: 'Christina Aguilera',year: 2000,month: 6,day: 15,weeksAtNumberOne: 2,duration: '3:20',},{name: 'I Knew I Loved You',artist: 'Savage Garden',year: 2000,month: 8,day: 29,weeksAtNumberOne: 4,duration: '4:10',},{name: 'Thank God I Found You',artist: 'Mariah Carey featuring Joe and 98 Degrees',year: 2000,month: 2,day: 19,weeksAtNumberOne: 2,duration: '4:17',},{name: 'Amazed',artist: 'Lonestar',year: 2000,month: 7,day: 4,weeksAtNumberOne: 2,duration: '4:00',}]

function summerCount(arr) {
  return arr.reduce((init, next) => init + ([6,7,8].includes(next.month) ? 1 : 0)
  , 0)
}
console.log(summerCount(songs))
Mark
2018-07-30