Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/415.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么这个函数在使用includes方法时抛出错误?_Javascript - Fatal编程技术网

Javascript 为什么这个函数在使用includes方法时抛出错误?

Javascript 为什么这个函数在使用includes方法时抛出错误?,javascript,Javascript,我很难理解为什么这个函数在到达'includes'方法时抛出错误,当控制台记录白名单时,它显示它是一个数组,当我使用includes方法时,它抛出错误,无法读取未定义的属性includes const test = (body, ...whitelists) => { const bodies = whitelists.map(({ type = null, whitelist }) => { console.log('whitelist ', whitelist)

我很难理解为什么这个函数在到达
'includes'
方法时抛出错误,当控制台记录白名单时,它显示它是一个数组,当我使用includes方法时,它抛出错误,
无法读取未定义的属性includes

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
的第二个参数传递,然后尝试从此数组中提取
白名单
属性,这毫无意义

console.log('whitelist',whitelist)
按预期显示
未定义的
,因为
['firstKey']。白名单
未定义的


不确定您试图在此处执行什么操作。

这是因为您正在传递一个数组作为
测试的第二个参数,然后尝试从此数组中提取
白名单
属性,这毫无意义

console.log('whitelist',whitelist)
按预期显示
未定义的
,因为
['firstKey']。白名单
未定义的

不确定您在这里要做什么。

1)设置默认值=[]

  const bodies = whitelists.map(({ type = null, whitelist = [] }) => {

2) 将白名单替换为白名单

if (whitelists.includes(key)) {
1) 设置默认值=[]

  const bodies = whitelists.map(({ type = null, whitelist = [] }) => {

2) 将白名单替换为白名单

if (whitelists.includes(key)) {

这是可以关闭的,实际上是我的错,我错误地为函数调用的第二个参数传入了一个数组,它应该是一个对象。另外,应该在白名单而不是白名单上调用includes


实际上,它看起来像是
test({firstKey:'123'},{whitelist:['firstKey']})

这是可以关闭的,实际上是我的错,我错误地为函数调用的第二个参数传入了数组,它应该是一个对象。另外,应该在白名单而不是白名单上调用includes


实际上,它看起来像是
test({firstKey:'123},{whitelist:['firstKey']})

你的意思是
白名单.包括
?你的意思是
白名单.包括