0

I have a json array stored in a url like

http://localhost/heart/api/restApiController/dataset.json

Json array looks like this

[
    {
        Weight: "3",
        Smoking: "1",
        Exercising: "0",
        Food_habits: "0",
        Parents_HA: "1",
        Alcohol: "2",
        Occupation: "1",
        Working_hours: "4",
        Heart_Attack: "0"
    }, {
        Weight: "4",
        Smoking: "0",
        Exercising: "1",
        Food_habits: "0",
        Parents_HA: "1",
        Alcohol: "1",
        Occupation: "1",
        Working_hours: "2",
        Heart_Attack: "0"
    }, {
        Weight: "2",
        Smoking: "1",
        Exercising: "1",
        Food_habits: "0",
        Parents_HA: "1",
        Alcohol: "2",
        Occupation: "1",
        Working_hours: "4",
        Heart_Attack: "0"
    }
]

I want to count the number of objects in this array and how many objects are there which has Heart_attack:'0' value. How can I do this?

1
  • What have you tried so far? Please post an example of the relevant parts of your application. Commented May 22, 2016 at 6:02

2 Answers 2

1

Use .filter to get the array elements with Heart_attack = 0 and then apply .length

var arr; // Represent your array
arr.filter(function (item) { 
  return item.Heart_attack == 0; 
}).length;

Working example:

var arr = [
    {
        Weight: "3",
        Smoking: "1",
        Exercising: "0",
        Food_habits: "0",
        Parents_HA: "1",
        Alcohol: "2",
        Occupation: "1",
        Working_hours: "4",
        Heart_Attack: "0"
    }, {
        Weight: "4",
        Smoking: "0",
        Exercising: "1",
        Food_habits: "0",
        Parents_HA: "1",
        Alcohol: "1",
        Occupation: "1",
        Working_hours: "2",
        Heart_Attack: "0"
    }, {
        Weight: "2",
        Smoking: "1",
        Exercising: "1",
        Food_habits: "0",
        Parents_HA: "1",
        Alcohol: "2",
        Occupation: "1",
        Working_hours: "4",
        Heart_Attack: "0"
    }
];

var len = arr.filter(function (item) {
  return item.Heart_Attack == 0;
}).length;

document.write(len);

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

3 Comments

my array is not hard coded in my controller.. instead it's in the url I have given in the question. when it's in a url, how I can do this?
It's just an example assuming that you have the data. If you don't please provide the complete code you have
I have the data but it's in a database table. so how to count when it's in a DB table?
0

Make a ajax request to the url you mentioned and find the length of the array in the success callback of the request.

function callback (data) {
if (data) {
    var heartAttackCount = 0;
    console.log("Data Length: " + data.length);
    data.forEach(function (object) {
        if (object.hasOwnProperty('Heart_Attack') && object['Heart_Attack'] === "0") {
            ++heartAttackCount;
        }
    });
    console.log("Heart Attack Count: " + heartAttackCount);
}

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.