0

probably missing something obvious but my map function's index param will not increment up its just stuck as 0 or whatever static index I set it to, maybe I'm missing something because my data structure im passing in is an array of objects like:

(20) -> [{},{},{}]

and each object has quite a few properties I'll provide my map function below for greater context

also I can manually change my index and my code works fine grabbing an object at whatever index I specify i.e '5' or '13' and my depth is correct because its displaying the property values as it should, wondering if I need to nest something to make this work?

if I console log my state I have this structure here just an array of objects enter image description here

//storing to state

componentDidMount(){
        
        fetch(`https://api.nasa.gov/neo/rest/v1/feed?start_date=${this.state.time[0]}&end_date=${this.state.time[1]}&api_key=ipAxYzaENbqRKb7GgzFPcH6QUBsHXY3QKB7uXOf5`
        )
      .then(response => response.json())
      .then((data) => {
        let ast = []
        Object.values(data.near_earth_objects).forEach((arr)=>{
            //push the two arrays together
            ast.push(...arr)
        })
        this.setState({asteroids:[ast]})
       
      });
    }





render() {
        return (
            <div>
                <h4>Near earth objects</h4>

                <table>
                    <tbody>
                    <tr>
                        <th>Name</th>
                        <th>ID</th>
                        <th>Magnitude</th>
                        <th>Hazardous</th>
                        <th>Sentry Object</th>
                    </tr>
                  
                { 
                
                this.state.asteroids.map((arr,index)=>(
                    //for each item (arr) there will be properties
                    
                <tr key={arr.toString()}>
                    <td >{arr[index].name}</td>
                    <td >{arr[index].id}</td>
                    <td >{arr[index].absolute_magnitude_h}</td>
                    <td >{arr[index].is_potentially_hazardous_asteroid==true? "true": "false"}</td>
                    <td >{arr[index].is_sentry_object==true? "true": "false"}</td>
                    <td > index {index}</td>
                </tr> 
                
                ))  
                 
            }
            </tbody>
            </table>
                
            </div>
        )
    }
2
  • If I am not wrong your this.state.asteroids is an array of object . If that is the case you can just do arr.name and arr.id . Commented Aug 27, 2021 at 18:00
  • I tried that a bit ago but it seems to just ignore the object altogether and renders nothing for the properties within map() I have updated the code with a clearer understanding of my object structure Commented Aug 27, 2021 at 18:24

1 Answer 1

1

The error is in componentDidMount when you setState of asteroids your taking the array and putting it inside another array

  this.setState({asteroids:[ast]})
  // instead it should be 
  this.setState({asteroids: ast })

Checkout this codesandbox with a working example of your code https://codesandbox.io/s/empty-forest-jvwin?file=/src/App.tsx

As for why the index is stuck, it's because the asteroids array in your example has only 1 element (the array that is holding all the asteroids data).

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

1 Comment

ah thanks that did the trick I completely missed that thank you for the example :)

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.