简体   繁体   English

难以遍历Arrays数组并在Redux Store中生成JSX中的动态React元素

[英]Having difficulty iterating through an Object of Arrays and generating dynamic React elements in JSX from within the Redux Store

I am fetching some data from an API and organizing it into an Object of Arrays - Each Array is an Array of Objects itself 我从API获取一些数据并将其组织成一个Arrays Object - 每个Array本身就是一个Objects Array

const menuItems = { 
    Dinner: [{_id: "2af4", itemName: "Filtet Mignon"}],
    Fruit: [{_id: "41gg", itemName: "Strawberries"}, {_id: "2fas", itemName: "Apple"}, {_id: "gkkm", itemName: "Pear"}],
    Lunch: [{_id: "fmuu" itemName:"Artichoke Pizza"}]

}

These items are fetched from the API and stored in the Redux Store. 这些项目从API获取并存储在Redux Store中。

I am successfully using connect(mapStateToProps)(MyComponent) to bring these items within the scope of my Component's bound functions. 我成功地使用connect(mapStateToProps)(MyComponent)将这些项目放在Component的绑定函数的范围内。

I would like to create a <div> or <button> element for each item, but my methods do not seem to be working. 我想为每个项目创建一个<div><button>元素,但我的方法似乎不起作用。 This is what I have tried: 这是我尝试过的:

iterateThruObject() {
    const { menuItems } = this.props

    return Object.keys(menuItems).forEach((k) => { 
        return menuItems[k].map(item => <div key={item._id}>{item.itemName}</div>)
    })
}

render() {
 const { menuItems } = this.props
  return(
     {menuItems && this.iterateThruObject()}
  )}

As far as I can see - this should be working. 据我所知 - 这应该有效。 The iterateThruObject() is being properly triggered, and if I put console.log() inside, I can see all of the different properties being cycled through. 正在正确触发iterateThruObject() ,如果我将console.log()放入其中,我可以看到所有不同的属性都在循环中。 So I confused why the <div> elements are not being created. 所以我很困惑为什么没有创建<div>元素。

I have tried to use a for...in loop to iterate through the Object, but the loop terminates early at the first return 我试图使用for...in循环迭代Object,但循环在第一次return提前终止

If anyone can help me with this, point me to my mistake, I would really appreciate it. 如果有人能帮助我,请指出我的错误,我真的很感激。

Here are the console.log entries so the data structure is easier to visualize: 以下是console.log条目,因此数据结构更易于可视化:

console.log(Object.keys(menuItems))
// ["Dinner", "Fruit", "Lunch"]

 console.log(menuItems[k]) // 3 Invocations within .forEach()
// Array [{_id: "2af4", itemName: "Filtet Mignon"}]
// Array [{_id: "41gg", itemName: "Strawberries"}, {_id: "2fas", itemName: "Apple"}, {_id: "gkkm", itemName: "Pear"}]
// Array [{_id: "fmuu" itemName:"Artichoke Pizza"}]

menuItems[k].map(item => console.log(item.itemName)) // 3 .map() Invocations
// Filet Mignon - Iteration 1
// Strawberries - Iteration 2
// Apple - Iteration 2
// Pear - Iteration 2
// Artichoke Pizza - Iteration 3

Just use .map instead of forEach so an object will get returned: 只需使用.map而不是forEach ,就会返回一个对象:

return Object.keys(menuItems).map((k) => {
  return menuItems[k].map(item => <div key={item._id}>{item.itemName}</div>)
})

Note that forEach will always return undefined , unlike map or reduce : 请注意, forEach 将始终返回undefined ,与map或reduce不同

forEach() executes the callback function once for each array element; forEach()为每个数组元素执行一次回调函数; unlike map() or reduce() it always returns the value undefined and is not chainable. 与map()或reduce()不同,它总是返回undefined值并且不可链接。

Running example: 运行示例:

 const data = { Dinner: [{ _id: "2af4", itemName: "Filtet Mignon" }], Fruit: [{ _id: "41gg", itemName: "Strawberries" }, { _id: "2fas", itemName: "Apple" }, { _id: "gkkm", itemName: "Pear" }], Lunch: [{ _id: "fmuu", itemName: "Artichoke Pizza" }] } class App extends React.Component { iterateThruObject = () => { const { menuItems } = this.props return Object.keys(menuItems).map((k) => { return menuItems[k].map(item => <div key={item._id}>{item.itemName}</div>) }) } render() { const { menuItems } = this.props; return ( <div > {menuItems && this.iterateThruObject()} </div> ); } } ReactDOM.render(<App menuItems={data} />, document.getElementById('root')); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM