0

Hi I have this list array of object in react js and I don't know how to display in my render view. Anyone can give an idea on how to do it?

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"}
  ]
}

This is the sample output:

**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

I have this code and I've got an error this.state.post.map is not a function

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

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

Thank you

5
  • You will have to loop over data and create an array of React.NodeElement. Then just put it in render Commented Nov 15, 2018 at 5:42
  • We will be able to help you quicker if you could share an attempt that did not work Commented Nov 15, 2018 at 5:44
  • updated @Ahmad and rajest Commented Nov 15, 2018 at 5:50
  • can you post you render function? Commented Nov 15, 2018 at 5:59
  • @Robsonsjre updated thanks Commented Nov 15, 2018 at 6:01

2 Answers 2

2

This is how you map it. just change post with 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>
       )
  })
*/

Sign up to request clarification or add additional context in comments.

4 Comments

OP needs access to top level keys like Store1 but you are throwing them out
Well I thought he wanted to render list of Businesses
Yes Store1 (businesses was removed from the array
@DelgeeB You must have missed the desired output the OP posted
1

map is not a method of an object. You can map over its keys using Object.keys.

render() {
   const groupList = Object.keys(this.state.post).map((key) => {
       return  (
           <div key={key}>
              <div>{this.state.post[key]}</div>
           </div>
       )
   });

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

However, there are other problems once you fix that but you should try to solve them yourself and ask other questions if you can't

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.