0

How to Make a Accessing data in an array, and list separated by categories Using axios React Native

I am trying to deploy a list with categories and products using axio in react native

my json structure

  [
      {
        "id": "1",
        "name": "TV",
        "list": [
          {
            "idp": "1",
            "namep": "TV 43"
          },
          {
            "idp": "2",
            "namep": "TV 32"
          }
        ]
      },
      {
        "id": "2",
        "name": "Couch",
        "list": [
          {
            "idp": "3",
            "namep": "Couch for 3 people"
          },
          {
            "idp": "4",
            "namep": "Couch for 2 people"
          }
        ]
      }
    ]

what I've already done, so I can display categories

    constructor(props) {
       super(props);
       this.state = { category: [], list: [] };
       }

    componentWillMount() {
                axios.get('http://localhost/api/list.json')
               .then(response => {
                 this.setState({ 

             category: response.data, 
             list: response.data.list });

             })
               .catch(() => { console.log('Err'); });
           } 
.............

{this.state.category.map((item, i) => {
<Text>{item.name}</Text>
  { item.list.map((product, i) => {
       <Text>{product.namep}</Text>
  })}
 })}

Example list

2
  • What do you mean with "Split List"? Commented Mar 18, 2018 at 1:04
  • Accessing data in an array, and list separated by categories @Oxcarga Commented Mar 18, 2018 at 1:14

1 Answer 1

1

Sorry I can't give you a more detailed and focused answer since I'm not familiar with react native but logic goes like this:

A loop to iterate over main array and extract categories names then another loop inside that one for iterating over products.

const arr = [
      {
        "id": "1",
        "name": "TV",
        "list": [
          {
            "idp": "1",
            "namep": "TV 43"
          },
          {
            "idp": "2",
            "namep": "TV 32"
          }
        ]
      },
      {
        "id": "2",
        "name": "Couch",
        "list": [
          {
            "idp": "3",
            "namep": "Couch for 3 people"
          },
          {
            "idp": "4",
            "namep": "Couch for 2 people"
          }
        ]
      }
    ];

const parentEl = document.querySelector('#list');


arr.forEach((cat) => {
	const catEl = document.createElement('div')
  catEl.textContent = cat.name
  parentEl.append(catEl)
  
  cat.list.forEach((prod) => {
    const listEl = document.createElement('ul')
    const listItem = document.createElement('li')
    listItem.textContent = prod.namep
    listEl.append(listItem)
    parentEl.append(listEl)
  })
})
<div id="list">
  
</div>

So, not knowing react native AND assuming this code you pasted is correct, I'm going to risk saying it would go something like this:

{
  this.state.category.forEach((item, i) => {
    <Text>{item.name}</Text>
    item.list.forEach((product, i) => {
      <Text>{product.namep}</Text>
    })
  })
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I adjusted the logic in ReactNative and it worked perfectly.

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.