javascript - 二维数组使用includes无效(cannot read property 'includes' of undefined)
PHPz
PHPz 2017-04-11 12:10:16
[JavaScript讨论组]

程序的目的是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]也应该是一个数组才对啊,怎么会出现这种情况呢?

求大神解答,谢谢。

PHPz
PHPz

学习是最好的投资!

全部回复(2)
阿神

result[0] 是数组,请问 result[1]是什么?

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

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号