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

问题描述 投票:0回答:1

我试图解决包括未定义的问题,为此,我正在使用&&运算符。

 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. 

我不需要真值,对于初始安装的组件,我需要假值。

我的问题是我该如何解决无法通过使用&&运算符或任何内容读取未定义的属性'includes'。这是我的代码沙箱:https://codesandbox.io/s/stackoverflow-a-60764570-3982562-v1-um18k

javascript arrays reactjs object
1个回答
1
投票

代替某些,您可以使用每一项来检查全部。

isAllChecked = label => {
    const { permission } = this.state;
    const data = groupItems.every(
      x => permission && permission[label] && permission[label].includes(x)
    );
    return data;
  };
© www.soinside.com 2019 - 2024. All rights reserved.