Javascript JS Array.reduce:遇到;未捕获的TypeError:无法读取未定义“的属性[…]”;

Javascript JS Array.reduce:遇到;未捕获的TypeError:无法读取未定义“的属性[…]”;,javascript,arrays,class,Javascript,Arrays,Class,我正在试着完成一个关于的练习。我想知道为什么我提出的解决方案不起作用 这个想法很简单。您有一个网页,其中视频的持续时间存储在列表中,如下所示: <ul class="videos"> <li data-time="5:43"> Video 1 </li> <li data-time="2:33"> Video 2 </li> </ul> 我的(尚未运行)解决方

我正在试着完成一个关于的练习。我想知道为什么我提出的解决方案不起作用

这个想法很简单。您有一个网页,其中视频的持续时间存储在列表中,如下所示:

  <ul class="videos">
    <li data-time="5:43">
      Video 1
    </li>
    <li data-time="2:33">
      Video 2
    </li>
  </ul>
我的(尚未运行)解决方案:

function Time(hours = 0, minutes = 0, seconds = 0) {
    this.hours = hours;
    this.minutes = minutes;
    this.seconds = seconds;
    this.addTime = function(hours = 0, minutes = 0, seconds = 0) {
      const ajout = new Time(hours, minutes, seconds);
      this.add(ajout);
    }
    this.add = function(ajout) {
      let surplus = 0;
      // seconds
      this.seconds += ajout.seconds;
      while (this.seconds >= 60) {
        this.seconds -= 60;
        surplus++;
      }
      // minutes
      this.minutes += ajout.minutes + surplus;
      surplus = 0;
      while (this.minutes >= 60) {
        this.minutes -= 60;
        surplus++;
      }
      // hours
      this.hours += ajout.hours + surplus;
    }
    this.toString = function() {
      return `${this.hours} h ${this.minutes} min ${this.seconds} s`;
    }
  }

  function stringToTime (str) {
    let h, m, s = 0;
    const arr = str.split(':');
    while (arr.length > 3) {
      arr.shift();
    }
    if (arr.length == 3) {
      h = parseInt(arr.shift());
    }
    if (arr.length == 2) {
      m = parseInt(arr.shift());
    }
    if (arr.length == 1) {
      s = parseInt(arr.shift());
    }

    return new Time(h, m, s);
  }
  //const reducerForTime = (accumulator, currentValue = new Time()) => accumulator.add(currentValue) || new ();
  const reducerForTime = (accumulator, currentValue = new Time()) =>
    {
      console.log(currentValue);
      //console.log(`accumulator = ${accumulator.toString()}; currentValue = ${currentValue.toString()}`);
      accumulator.add(currentValue);
      console.log(`accumulator after adding = ${accumulator.toString()}`);
    };

  const videos = document.querySelectorAll('ul.videos li');
  let videoTimes = [];
  for (let i = 0; i < videos.length; i++) {
    videoTimes.push(stringToTime(videos[i].dataset.time));
  }
  console.log(videoTimes.reduce(reducerForTime).toString());
功能时间(小时=0,分钟=0,秒=0){
这个.小时=小时;
这个。分钟=分钟;
这个。秒=秒;
this.addTime=函数(小时=0,分钟=0,秒=0){
const ajout=新时间(小时、分钟、秒);
这个。添加(ajout);
}
this.add=函数(ajout){
设盈余=0;
//秒
this.seconds+=ajout.seconds;
而(this.seconds>=60){
这是0.5秒-=60秒;
盈余++;
}
//会议记录
this.minutes+=ajout.minutes+剩余;
盈余=0;
而(this.minutes>=60){
这1.5分钟-=60;
盈余++;
}
//小时数
this.hours+=ajout.hours+剩余时间;
}
this.toString=函数(){
返回`${this.hours}h${this.minutes}min${this.seconds}s`;
}
}
函数stringToTime(str){
设h,m,s=0;
const arr=str.split(':');
而(arr.length>3){
arr.shift();
}
如果(arr.length==3){
h=parseInt(arr.shift());
}
如果(arr.length==2){
m=parseInt(arr.shift());
}
如果(arr.length==1){
s=parseInt(arr.shift());
}
返回新时间(h、m、s);
}
//const reducerForTime=(累加器,currentValue=new Time())=>accumulator.add(currentValue)| new();
常量reducerForTime=(累加器,currentValue=新时间())=>
{
console.log(当前值);
//log(`acculator=${acculator.toString()};currentValue=${currentValue.toString()}`);
累加器。添加(当前值);
log(`acculator after adding=${acculator.toString()}`);
};
const videos=document.queryselectoral('ul.videos li');
让videoTimes=[];
for(设i=0;i
错误如下: 未捕获类型错误:无法读取未定义的属性“add”

您会注意到,我在reducer函数中添加了两行“console.log”,以尝试找出哪里出了问题

现在,我得到这个控制台输出:

Time {hours: 0, minutes: 2, seconds: 33, addTime: ƒ, add: ƒ, …}
accumulator after adding = 0 h 8 min 16 s
Time {hours: 0, minutes: 3, seconds: 45, addTime: ƒ, add: ƒ, …}
Uncaught TypeError: Cannot read property 'add' of undefined
    at reducerForTime (index-START.html:239)
    at Array.reduce (<anonymous>)
    at index-START.html:248
reducerForTime @ index-START.html:239
(anonymous) @ index-START.html:248
Time{hours:0,minutes:2,seconds:33,addTime:ƒ,add:ƒ,…}
添加后的蓄能器=0小时8分钟16秒
时间{hours:0,minutes:3,seconds:45,addTime:ƒ,add:ƒ,…}
未捕获的TypeError:无法读取未定义的属性“add”
在reducerForTime(index START.html:239)
在Array.reduce()处
在索引开始处。html:248
reducerForTime@index START.html:239
(匿名)@index START.html:248
它所指的行是248239。第248行是调用reduce函数的那一行。行239是带有
累加器的行。添加(currentValue)。我不知道为什么is不工作,因为根据我的理解,累加器应该从一个迭代到下一个迭代,因此应该是一个时间对象,因此应该有一个“add”属性。为什么错误消息告诉我它未定义

累加器应从一个迭代到下一个迭代

累加器等于在上一次迭代中返回的值。在ReduceTime中,您当前没有返回任何内容。将其更改为:

const reducerForTime = (accumulator, currentValue = new Time()) => {
  accumulator.add(currentValue);
  return accumulator;
};

尝试
返回累加器在<代码>减速机端口<代码>功能的末尾当然!现在我明白了。减速器的定义与任何其他函数一样,因此需要返回值。非常感谢。
const reducerForTime = (accumulator, currentValue = new Time()) => {
  accumulator.add(currentValue);
  return accumulator;
};