Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
42
rated 0 times [  47] [ 5]  / answers: 1 / hits: 23820  / 7 Years ago, sun, september 3, 2017, 12:00:00

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);
});
}

More From » javascript

 Answers
18

Count the keys



  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;

[#56594] Wednesday, August 30, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dewayneh

Total Points: 538
Total Questions: 114
Total Answers: 97

Location: Liberia
Member since Fri, Oct 22, 2021
3 Years ago
dewayneh questions
Sat, Sep 4, 21, 00:00, 3 Years ago
Sun, Nov 1, 20, 00:00, 4 Years ago
Wed, Sep 23, 20, 00:00, 4 Years ago
Fri, Aug 7, 20, 00:00, 4 Years ago
Wed, Jul 29, 20, 00:00, 4 Years ago
;