我一直在尝试在javascript中的数组上使用map方法,这是我一直在使用的数组的快照。
[
{"name":"supplier","elements":["beta","gama","alpha"]},
{"name":"commodity","elements":[]},
{"name":"year","elements":[]}
]
这是我对 map 的使用
function renderTable(entityMap){
return entityMap.map((lookup,index)=>{
console.log(lookup)
const {name, elements} = lookup
return (
<tr key={index}>
<td>{name}</td>
<td>{elements}</td>
</tr>
)
})
}
我知道我使用的是正确的数组,因为当我这样做时
console.log(entityMap[1])
它返回{name: "commodity", elements: Array(0)}
更新
我正在 render() 中调用 renderTable 函数
class LookupTable extends Component {
render() {
const {
close,
saveAndClose,
entityNames,
entityModal,
entityMap
} = this.props
console.log(entityMap[1])
return (
<Modal
title='Add Lookups for entities'
visible={Boolean(entityModal)}
onOk={() => saveAndClose()}
onCancel={() => close()}
okText='save'
>
<div>
<table>
<tbody>
{renderTable({entityMap})}
</tbody>
</table>
</div>
</Modal>
)
}
}
更新2
Array.isArray(entityMap) 返回 true
最佳答案
您需要删除调用 renderTable 处的大括号,因为 map 方法适用于数组而不是对象。
我在需要删除大括号的地方添加了代码,希望它会有所帮助
<tbody>
{renderTable(entityMap)}
</tbody>
关于javascript - .map 不是 javascript 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60145279/