为什么在使用includes方法时此函数会引发错误?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么在使用includes方法时此函数会引发错误?相关的知识,希望对你有一定的参考价值。

[我很难弄清楚为什么此函数到达'includes'方法时会引发错误,当控制台记录白名单时,它表明它是一个数组,当我使用include方法时,会引发错误, cannot read property includes of undefined

const test = (body, ...whitelists) => 
  const bodies = whitelists.map(( type = null, whitelist ) => 
    console.log('whitelist ', whitelist)

    const whitelistedBody = Object.entries(body).reduce(
      (newBody, [key, value]) => 
        console.log('KEY ', key)
        console.log('whitelist ', whitelists)

        if (whitelist.includes(key)) 
          newBody[key] = value;
        

        console.log('newBody ', newBody)
        return newBody;
      ,
      
    );

    return  type, body: whitelistedBody ;
  );

  return (
    bodies.find(( body ) => Object.keys(body).length) || 
      body: ,
      type: null,
    
  );
;

test(firstKey: '123', ['firstKey']);
答案

这是因为要传递数组作为test的第二个参数,然后尝试从此数组中提取whitelist属性,这没有任何意义。

以上是关于为什么在使用includes方法时此函数会引发错误?的主要内容,如果未能解决你的问题,请参考以下文章