javascript - 在React JS中显示对象的数组列表

标签 javascript arrays reactjs

嗨,我在 React js 中有这个对象列表数组,但我不知道如何在渲染 View 中显示。任何人都可以提供如何做到这一点的想法吗?

Post:{
  Store1: [
     0:{Id:"0001", Business:"Ministop"}
   ]
  Store2: [{
     0:{Id:"0002", Business:"Grocery Store"}
  }]
  Store3: [
     0:{Id:"0003", Business:"Seven Eleven"}
  ]
  Store4: [
     0:{Id:"0004", Business:"Big Store"},
     1:{Id:"0005", Business:"Medium Store"}
  ]
}

这是示例输出:

**Store 1**
   **Id      Business**
   0001    Ministop
**Store 2**
   **Id      Business**
   0002    Grocery Store
**Store 3**
   **Id      Business**
   0003    Seven Eleven
**Store 4**
   **Id      Business**
   0004    Big Store
   0005    Medium Store

我有这段代码,但出现错误 this.state.post.map 不是函数

render() {
   const groupList = this.state.post.map((data, index) => {
       return  (
           <div key={index}>
              <div>{data}</div>
           </div>
       )
   });

   return (
     <div>{groupList}</div>
   )
} 

谢谢

最佳答案

这就是您映射它的方式。只需使用 this.state.post 更改帖子

const post = {
  Store1: [
    { Id: '0001', Business: 'Ministop' }
  ],
  Store2: [
    { Id: '0002', Business: 'Grocery Store' }
  ],
  Store3: [
    { Id: '0003', Business: 'Seven Eleven' }
  ],
  Store4: [
    { Id: '0004', Business: 'Big Store' },
    { Id: '0005', Business: 'Medium Store' }
  ]
};

console.log(Object.keys(post).reduce((acccumilator, iterator) => {
  return [...acccumilator, ...post[iterator]];
}, []));

/*
  Object.keys(this.state.post).reduce((acccumilator, iterator) => {
    return [...acccumilator, ...post[iterator]];
  }, []).map(data => {
    return  (
           <div key={data.id}>
              <div>{data.Business}</div>
           </div>
       )
  })
*/

关于javascript - 在React JS中显示对象的数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53313078/

相关文章:

reactjs - getInitialProps 永远不会被调用...我做错了什么?

javascript - 使用 enzyme 获取子元素

javascript - 对于循环闭合

javascript - IndexedDB:在特定条目上打开游标而不进行迭代

javascript - 如何在sublime上添加未定义数量的占位符的js片段

jquery - 获取全名数组并按姓氏排序

javascript - 为什么console.log 在作为参数传递给forEach 时不起作用?

javascript - 类型 'pop' 上不存在属性 'NavigationProp<Record<string, object | undefined>, string, NavigationState, {}, {}>'

javascript - React : this. props.posts.map 不是一个函数

Javascript向对象添加方法