gpt4 book ai didi

javascript - 无法读取reactjs中未定义的属性 'includes'?

转载 作者:行者123 更新时间:2023-12-02 21:23:49 25 4
gpt4 key购买 nike

我尝试解决包含未定义的问题,为此,我使用 && 运算符。

 isAllChecked = label => {
const { permission } = this.state;

false value - I need false value when I get data from API,
But I got an error that includes undefined. for that, I used && operator
but I got a true value, I don't need to get true value

const data = !groupItems.some(
x => !permission && !permission[label] && !permission[label].includes(x)
);

// console.log(true) //true

const data = !groupItems.some(
x => !permission[label].includes(x)
);

// I got a false value using static data without using && operator

// data output: false (accepted output- getting using static data, but I need
to get the same value when I get data from API, I got an error includes undefined without using && operator)

return data;

};

但是,如果我从 API 获取数据,则显示此方法无法读取未定义的属性,并且当我要解析未定义的“包含”时,我使用了 && 运算符,并且得到了真实值。

 Cannot read property 'includes' of undefined. 

我不需要 true 值,我需要初始安装的组件的 false 值。

我的问题是如何解决 Cannot read property 'includes' of undefined by using && 运算符或其他内容。
这是我的代码沙箱:https://codesandbox.io/s/stackoverflow-a-60764570-3982562-v1-um18k

最佳答案

您可以使用 every 来检查全部,而不是 some。

isAllChecked = label => {
const { permission } = this.state;
const data = groupItems.every(
x => permission && permission[label] && permission[label].includes(x)
);
return data;
};

关于javascript - 无法读取reactjs中未定义的属性 'includes'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60788701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com