Javascript Resolve forEach不是一个函数
我有一个对象数组作为道具传递到子组件中,我想在其中显示列表中的项目 data props.itemAssociations如下所示:Javascript Resolve forEach不是一个函数,javascript,reactjs,Javascript,Reactjs,我有一个对象数组作为道具传递到子组件中,我想在其中显示列表中的项目 data props.itemAssociations如下所示: [ {source: "hello world", target: "my item"}, {source: "my item", target: "hello world"} ] 然后我尝试从道具创建列表项: let options = props.itemAssociations.map(items => items.forEach
[
{source: "hello world", target: "my item"},
{source: "my item", target: "hello world"}
]
然后我尝试从道具创建列表项:
let options = props.itemAssociations.map(items =>
items.forEach((item, index) => (
<li key={index} value={item.source}>
{item.source}
</li>
))
);
这将导致items.forEach不是一个函数。正在抛出此错误的任何提示/解决方案?.map已在数组上迭代,因此每个项都是其中一个对象。不需要做这件事
让options=props.itemsociations.mapitem,index=>
{item.source}
.forEach是一个数组方法。itemAssociations数组的内容是对象,因此它们没有.forEach。可以使用Object.entries将属性转换为数组。编辑,除非你根本不需要这样做;根本不需要像forEach这样的东西。