Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140676

Why does this function throw an error when using the includes method?

$
0
0

I'm having trouble figuring out why this function throws an error when it reaches the 'includes' method, when console logging whitelist, it shows that it is an array, when i use the includes method, it is throwing an error, 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']);

Viewing all articles
Browse latest Browse all 140676

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>