为了账号安全,请及时绑定邮箱和手机立即绑定

二维数组使用includes无效(cannot read property 'include

二维数组使用includes无效(cannot read property 'include

月关宝盒 2018-10-10 14:15:38
程序的目的是arr[i][1](也就是字符串)去重,并把去重后的结果插入result内,并返回,我的代码如下:var arr = [ [ 88, 'Bowling Ball' ],  [ 2, 'Dirty Sock' ],  [ 3, 'Hair Pin' ],  [ 12, 'Toothpaste' ],  [ 2, 'Hair Pin' ],  [ 3, 'Half-Eaten Apple' ],  [ 7, 'Toothpaste' ],  [ 2, 'Hair Pin' ],  [ 3, 'Half-Eaten Apple' ],  [ 67, 'Bowling Ball' ],  [ 7, 'Toothpaste' ],  [ 3, 'Half-Eaten Apple' ],  [ 67, 'Bowling Ball' ],  [ 7, 'Toothpaste' ],  [ 2, 'Hair Pin' ],  [ 3, 'Half-Eaten Apple' ],  [ 67, 'Bowling Ball' ] ];var result = [[0,'test']];arr.forEach(function(e,i){    if(!result[i].includes(e[1])){    result.push(e);  }  });  console.log(result);可是在调试的时候,系统给我的反馈是:TypeError: Cannot read property 'includes' of undefined我不是很明白,因为result[i]也应该是一个数组才对啊,怎么会出现这种情况呢?求大神解答,谢谢。
查看完整描述

1 回答

?
智慧大石

TA贡献1946条经验 获得超3个赞

!result[i].includes(e[1])

大哥,你这result在位置0后边都是undefined了,当然TypeError: Cannot read property 'includes' of undefined


查看完整回答
反对 回复 2018-11-20
  • 1 回答
  • 0 关注
  • 1263 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号