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']})
你的意思是白名单.包括
?你的意思是白名单.包括
?