我试图解决包含未定义的问题,为此,我正在使用&&运算符。
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.
我不需要真值,对于初始安装的组件,我需要假值。
我的问题是我该如何解决无法通过使用&&运算符或任何内容读取未定义的属性“包含”。
这是我的代码沙箱:https://codesandbox.io/s/stackoverflow-a-60764570-3982562-v1-um18k
答案 0 :(得分:1)
您可以使用所有人来代替全部检查。
isAllChecked = label => {
const { permission } = this.state;
const data = groupItems.every(
x => permission && permission[label] && permission[label].includes(x)
);
return data;
};