0

I'm using an API to get an array of Objects. Each object has a lot of data, I want to filter this data to just grab the data I need so I can display it on a React-Table.

export default class Table extends React.Component {
  constructor(props){
    super(props);
    this.state = {
    }
  }

  fetchData() {
  const string = 'http://localhost:8000/issues/assigned/mike';
  fetch(string)
  .then(function(response) {
    return response.json();
  })
  .then((myJson) => this.setState(myJson));
  console.log(this.state)
}

  componentDidMount(){
    this.fetchData();
  }

  componentDidUpdate(prevProps) {
    if (prevProps.value !== this.props.value) {
        this.fetchData()
    }
  }

render() {
    return this.state.issues? (
      <div>
        <ResponseTable data={this.state.issues} />
      </div>
    ) : (
      <div>
         Loading ...
      </div>
    );
  }
}

The JSON file I'm receiving from the API: JSON DATA NEST

For the example there is only one object, I'm receiving 50 object with the exact same nesting, I'm looking forward to extract a few properties (for example, data.issues[0].fields.timespent ) so I can pass this data into my react-table and create a row for each "issue".

2
  • What data are you trying to filter for? Commented May 4, 2018 at 9:37
  • Hi Colin, this is the data which i receive from the API, jsonblob.com/8e148758-4f83-11e8-9df1-01379195c3a0 For the example purpose the "Issues" only has one object.. i'm looking to filter some info, for example, timespent, description... Commented May 4, 2018 at 10:12

2 Answers 2

1

setState function does not immediately update a component but you can use a callback function setState(updater[, callback]) to get your state right after it was updated.

Regarding data filtering, you can use .map() or .filter() function to transform or filter your collection after a response was converted to JSON.

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

Comments

0

You can use .filter() method to filter data that you need and .map() method to map this data to appropriate model.

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.