Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Cascader Ant Design中显示子对象?_Javascript_Reactjs - Fatal编程技术网

Javascript 如何在Cascader Ant Design中显示子对象?

Javascript 如何在Cascader Ant Design中显示子对象?,javascript,reactjs,Javascript,Reactjs,我在如何在console.log下面显示categorySubResponses和categorySubChildResponses中的数据时遇到问题 像这样的梦境来自虚拟数据 现在没有孩子了 我使用ant设计https://ant.design/components/cascader/ 我将代码放入codesanbox.io和链接中 https://codesandbox.io/embed/q-56619059-so-8dk0z 您需要.map()categorySubResponse

我在如何在console.log下面显示categorySubResponses和categorySubChildResponses中的数据时遇到问题

像这样的梦境来自虚拟数据

现在没有孩子了

我使用ant设计
https://ant.design/components/cascader/

我将代码放入
codesanbox.io
和链接中

https://codesandbox.io/embed/q-56619059-so-8dk0z
您需要
.map()
categorySubResponses
categorySubResponses
为每个数组创建值和标签

const options = this.state.allCategory.map(category => ({
  value: category.id,
  label: category.name,
  children: category.categorySubResponses && category.categorySubResponses.map(child => ({
    value: child.id,
    label: child.name,
    children: child.categorySubChildResponses && child.categorySubChildResponses.map(innerChild => ({
      value: innerChild.id,
      label: innerChild.name
    })) || []
  })) || []
}));
代码清理

createSubChildren = (children = []) => {
  return (
    (children &&
      children.map(child => ({
        value: child.id,
        label: child.name
      }))) ||
    []
  );
};

createChildren = (children = []) => {
  return (
    children &&
    children.map(child => ({
      value: child.id,
      label: child.name,
      children: this.createSubChildren(child.categorySubChildResponses)
    }))
  ) || [];
};

const options = this.state.allCategory.map(category => ({
  value: category.id,
  label: category.name,
  children: this.createChildren(category.categorySubResponses)
}));

您需要
.map()
categorySubResponses
categorySubResponses
为每个数组创建值和标签

const options = this.state.allCategory.map(category => ({
  value: category.id,
  label: category.name,
  children: category.categorySubResponses && category.categorySubResponses.map(child => ({
    value: child.id,
    label: child.name,
    children: child.categorySubChildResponses && child.categorySubChildResponses.map(innerChild => ({
      value: innerChild.id,
      label: innerChild.name
    })) || []
  })) || []
}));
代码清理

createSubChildren = (children = []) => {
  return (
    (children &&
      children.map(child => ({
        value: child.id,
        label: child.name
      }))) ||
    []
  );
};

createChildren = (children = []) => {
  return (
    children &&
    children.map(child => ({
      value: child.id,
      label: child.name,
      children: this.createSubChildren(child.categorySubChildResponses)
    }))
  ) || [];
};

const options = this.state.allCategory.map(category => ({
  value: category.id,
  label: category.name,
  children: this.createChildren(category.categorySubResponses)
}));