I would like to count the length of my response JSON. Here's my code:
getMoviesFromApiAsync() {
return fetch('http://sampleurl.com/' + CommonDataManager.getInstance().getUserID())
.then((response) => response.json())
.then((responseJson) => {
this.setState({isLoading: false, listMessages: responseJson});
})
.catch((error) => {
console.error(error);
});
}
var myObject = {'key':'something', 'other-key':'something else','another-key': 'another thing'}
var count = Object.keys(myObject).length;
Where myObject
is your json response and count
is the length of your object
This is the best way to count the json objects correctly
In your code you should add this in your second .then
var count = Object.keys(responseJson).length;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With