Javascript 如何在Cascader Ant Design中显示子对象?
我在如何在console.log下面显示categorySubResponses和categorySubChildResponses中的数据时遇到问题 像这样的梦境来自虚拟数据 现在没有孩子了 我使用ant设计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
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)
}));