Reputation: 881
I am returning a JSON array for a project. Here's what the response looks like:
{"data":[{"DT_RowId":"row_60","contacts":{"first_name":"Screech","last_name":"Powers"},"users_todos":{"text":"whowho","due_date":"Wed, Feb 6 2019","company_id":"1","location_id":"15","assigned_to_user_id":"21","assigned_to_contact_id":"4258","inserted_by_user_id":"15","status_id":"0"}}],"options":[],"files":[]}
data: [{DT_RowId: "row_60", contacts: {first_name: "Screech", last_name:
"Powers"},…}]
0: {DT_RowId: "row_60", contacts: {first_name: "Screech", last_name:
"Powers"},…}
DT_RowId: "row_60"
contacts: {first_name: "Screech", last_name: "Powers"}
first_name: "Screech"
last_name: "Powers"
users_todos: {text: "whowho", due_date: "Wed, Feb 6 2019", company_id:
"1", location_id: "15",…}
assigned_to_contact_id: "4258"
assigned_to_user_id: "21"
company_id: "1"
due_date: "Wed, Feb 6 2019"
inserted_by_user_id: "15"
location_id: "15"
status_id: "0"
text: "whowho"
files: []
options: []
I am trying to return a function that will count how many of the results have users_todos -> status_id = 'value' and then put that into a label elsewhere on the page.
Here's what I have so far:
function count_category(category, json) {
var count= 0;
$.each(json, function (k , v) {
if (v['users_todos']['status_id'] === category) {
count++;
}
});
return count;
};
$('.user_todos_incomplete_count').text('(' + count_category('0', json['data']) + ')');
$('#user_todos_in_progress_count').text('(' + count_category('1', json['data']) + ')');
$('#user_todos_complete_count').text('(' + count_category('2', json['data']) + ')');
$('#user_todos_disregarded_count').text('(' + count_category('3', json['data']) + ')');
I am currently getting a count of 0 for all _count values. It seems like I'm missing the proper way to detect the status_id values from the JSON response.
Upvotes: 0
Views: 1510
Reputation: 61859
Assuming you parse your JSON into a JavaScript object first, then your code should work - it will return 1
for the first count, which is correct based on the sample data provided.
Just ensure you used JSON.parse() to import the JSON first, otherwise you're dealing with a string, and not an object.
function count_category(category, json) {
var count = 0;
$.each(json, function(k, v) {
if (v['users_todos']['status_id'] === category) {
count++;
}
});
return count;
};
var rawjson = '{"data":[{"DT_RowId":"row_60","contacts":{"first_name":"Screech","last_name":"Powers"},"users_todos":{"text":"whowho","due_date":"Wed, Feb 6 2019","company_id":"1","location_id":"15","assigned_to_user_id":"21","assigned_to_contact_id":"4258","inserted_by_user_id":"15","status_id":"0"}}],"options":[],"files":[]}';
var json = JSON.parse(rawjson);
console.log('(' + count_category('0', json['data']) + ')');
console.log('(' + count_category('1', json['data']) + ')');
console.log('(' + count_category('2', json['data']) + ')');
console.log('(' + count_category('3', json['data']) + ')');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
If you fail to parse the JSON and just run your code against the raw JSON string, it produces 0 for all counts, as you reported in your question.
N.B. There are definitely more efficient ways to achieve what you're trying to do, but my point is that there's nothing inherently broken about your code, apart from possibly not parsing your JSON.
Upvotes: -1
Reputation: 525
Well, to count all rows that have user_todos
you need another function like this one:
function count_all(json) {
return json.filter(function(row) {
return row.users_todos).length;
}
}
Upvotes: -1
Reputation: 50291
You can use array filter method , which will return an array of matched elements. Then use length
property to get the value
let dt = {
"data": [{
"DT_RowId": "row_60",
"contacts": {
"first_name": "Screech",
"last_name": "Powers"
},
"users_todos": {
"text": "whowho",
"due_date": "Wed, Feb 6 2019",
"company_id": "1",
"location_id": "15",
"assigned_to_user_id": "21",
"assigned_to_contact_id": "4258",
"inserted_by_user_id": "15",
"status_id": "0"
}
}],
"options": [],
"files": []
}
function getCount(cat) {
return dt.data.filter(elem => elem.users_todos.status_id === cat).length
}
console.log(getCount('0'))
Upvotes: 5