Home  >  Q&A  >  body text

javascript - 二维数组使用includes无效(cannot read property 'includes' of undefined)

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

求大神解答,谢谢。

PHPzPHPz2654 days ago892

reply all(4)I'll reply

  • 阿神

    阿神2017-04-11 12:12:16

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

    reply
    0
  • 黄舟

    黄舟2017-04-11 12:12:16

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

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

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-11 12:12:16

    字符串去重来了。。

    const 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' ] ];
    
    
    let hash={"test":0};
    arr.forEach((e)=>{
        if(!hash[e[0]]){
            hash[e[1]]=e[0];
        }
    });
    let result=[];
    for(let key in hash){
        const value=hash[key];
        result.push([value,key]);
    }
    
    console.log(result);

    -------------------------------分割线----------------------------------
    眼睛一花,看成序号去重。。将错就错的代码(序号去重)如下

    const 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' ] ];
    
    
    let hash={"0":"test"};
    arr.forEach((e)=>{
        if(!hash[e[0]]){
            hash[e[0]]=e[1];
        }
    });
    let result=[];
    for(let key in hash){
        const value=hash[key];
        result.push([key,value]);
    }
    
    console.log(result);

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-11 12:12:16

    开始没看清题意,是对数字和字符串都进行了比较。如果只比较字符串部分,只需要把判断中 && 和之前的部分去掉就可以了。

    两个内容相同但对象不是同一个的数组不能用 === 比较判等,所以 includes() 没法判断它们的相等性。可以用 every()some() 来逐个判断

    const result = arr
        .reduce((all, t) => {
            if (!all.some(x => t[0] === x[0] && t[1] === x[1])) {
                all.push(t);
            }
            return all;
        }, []);

    如果不要求按原顺序,可以先排序再判断,更快一些

    const result = arr
        .sort((a, b) => a[0] - b[0])
        .reduce((all, t) => {
            if (!all.length) {
                all.push(t);
            } else {
                const last = all[all.length - 1];
                if (!(last[0] === t[0] && last[1] === t[1])) {
                    all.push(t);
                }
            }
            return all;
        }, []);

    两段代码的结果:

    reply
    0
  • Cancelreply