开满天机
OP 中的代码将类似于[1, 2, 3, [4, 5, 6], [7, 8, 9]],我希望这不是您想要的。如果你想[1, 2, 3, 4, 5, 6, 7, 8, 9],使用这个:const containerNames = ["xContainers", "yContainers", "zContainers"]. flatMap(field => this.state.fileContent[field].map(...));如果需要[[1, 2, 3], [4, 5, 6], [7, 8, 9]],请将其更改flatMap为map。
哈士奇WWW
在mapToContainer函数中将c参数传递给您<Container containerName={c} />const allow = ['xContainers', 'yContainers', 'zContainers']const fileContent = { xContainers: [{name: '1'}, {name: '2'}, {name: '3'}], yContainers: [{name: '1'}, {name: '2'}, {name: '3'}], zContainers: [{name: '1'}, {name: '2'}, {name: '3'}], foo: {}}const mapToContainer = containers => containers.map(c => c.name)const containerNames = Object.keys(fileContent) .filter(k => allow.indexOf(k) !== -1) .reduce((prev, curr) => [...prev, ...mapToContainer(fileContent[curr])], [])console.log(containerNames)