Home > database >  Finding count of a specific variable from a JSON indexed object in JavaScript
Finding count of a specific variable from a JSON indexed object in JavaScript

Time:09-28

A LITTLE GUIDANCE WOULD HELP ME A LOT. My Data is unlabelled that's why I am facing problems. I have never worked with unlabelled data before.

I just need some help finding the total count of department_count from the below JSON object. I have tried many solutions and implemented my own logic but all the time I am failing to get the required results.

As there is a total of 3 departments so the output should be 3. Or the other way to find it is by adding the value of department_count in each iteration

THE REQUIRED RESULT IS 3

var myObject =[
{
  "school_id":"1",
  "school_name":"XYZ",
  "department_count":1,
  "department":[
     {
        "id":"stu1",
        "name":"sammy",
        "height":88,
        "weight":2,
        "bmi":1,
        "type":"Healthy"
     }
  ]
},
{
  "school_id":"2",
  "school_name":"ABC",
  "department_count":2,
  "department":[
     {
        "id":"stu2",
        "name":"Sam",
        "height":5,
        "weight":2,
        "bmi":1,
        "type":"Skinny"
     },
     {
        "id":"stu3",
        "name":"REd",
        "height":0,
        "weight":0,
        "bmi":0,
        "type":""
     }
   ]
}
]

CodePudding user response:

You just need a sumBy func to do it. Also I recommened you can take a look at this site 30 seconds of code, you will learn more from it.

const sumBy = (arr, fn) =>
  arr
    .map(typeof fn === 'function' ? fn : val => val[fn])
    .reduce((acc, val) => acc   val, 0);

var myObject =[
{
  "school_id":"1",
  "school_name":"XYZ",
  "department_count":1,
  "department":[
     {
        "id":"stu1",
        "name":"sammy",
        "height":88,
        "weight":2,
        "bmi":1,
        "type":"Healthy"
     }
  ]
},
{
  "school_id":"2",
  "school_name":"ABC",
  "department_count":2,
  "department":[
     {
        "id":"stu2",
        "name":"Sam",
        "height":5,
        "weight":2,
        "bmi":1,
        "type":"Skinny"
     },
     {
        "id":"stu3",
        "name":"REd",
        "height":0,
        "weight":0,
        "bmi":0,
        "type":""
     }
   ]
}
];

var results = sumBy(myObject, x => x.department_count);
console.log(results);

CodePudding user response:

assuming that the value for department_count is already set, you can just get the number of objects in the array as totalDepartments without iteration

var totalDepartments = myObject.length

CodePudding user response:

A counter with Duplicates check1

var schools =[
{
  "school_id":"1",
  "school_name":"XYZ",
  "department_count":1,
  "department":[
     {
        "id":"stu1",
        "name":"sammy",
        "height":88,
        "weight":2,
        "bmi":1,
        "type":"Healthy"
     }
  ]
},
{
  "school_id":"2",
  "school_name":"ABC",
  "department_count":2,
  "department":[
     {
        "id":"stu2",
        "name":"Sam",
        "height":5,
        "weight":2,
        "bmi":1,
        "type":"Skinny"
     },
     {
        "id":"stu3",
        "name":"REd",
        "height":0,
        "weight":0,
        "bmi":0,
        "type":""
     }
   ]
}
]
var schoolNames=[]
var count=0;
schools.forEach(school=>{
if(!schoolNames.includes(school.school_name))
{ schoolNames.push(school.school_name);count  ;
}})
console.log(count);

CodePudding user response:

You can use Array.prototype.reduce():

const myObject = [{school_id: '1',school_name: 'XYZ',department_count: 1,department: [{id: 'stu1',name: 'sammy',height: 88,weight: 2,bmi: 1,type: 'Healthy',},],},{school_id: '2',school_name: 'ABC',department_count: 2,department: [{id: 'stu2',name: 'Sam',height: 5,weight: 2,bmi: 1,type: 'Skinny',},{id: 'stu3',name: 'REd',height: 0,weight: 0,bmi: 0,type: ''}]}]

const total = myObject.reduce((a, { department_count }) => a   department_count, 0)

console.log(total)

CodePudding user response:

Many methods are there. basically what you need is a looping logic.

Array.reduce implmentation.

const myObject = [{"school_id":"1","school_name":"XYZ","department_count":1,"department":[{"id":"stu1","name":"sammy","height":88,"weight":2,"bmi":1,"type":"Healthy"}]},{"school_id":"2","school_name":"ABC","department_count":2,"department":[{"id":"stu2","name":"Sam","height":5,"weight":2,"bmi":1,"type":"Skinny"},{"id":"stu3","name":"REd","height":0,"weight":0,"bmi":0,"type":""}]}];
const sum = myObject.reduce((acc, curr) => {
  acc  = curr.department_count;
  return acc;
}, 0);
console.log(sum);

Edit: You could make Object destructuring aswell to make it a little more clean.

const myObject = [{"school_id":"1","school_name":"XYZ","department_count":1,"department":[{"id":"stu1","name":"sammy","height":88,"weight":2,"bmi":1,"type":"Healthy"}]},{"school_id":"2","school_name":"ABC","department_count":2,"department":[{"id":"stu2","name":"Sam","height":5,"weight":2,"bmi":1,"type":"Skinny"},{"id":"stu3","name":"REd","height":0,"weight":0,"bmi":0,"type":""}]}];
const sum = myObject.reduce((acc, {department_count}) => acc   department_count, 0);
console.log(sum);

CodePudding user response:

This may help you.

var myObject =[
   {
      "school_id":"1",
      "school_name":"XYZ",
      "department_count":1,
      "department":[
         {
            "id":"stu1",
            "name":"sammy",
            "height":88,
            "weight":2,
            "bmi":1,
            "type":"Healthy"
         }
      ]
   },
   {
      "school_id":"2",
      "school_name":"ABC",
      "department_count":2,
      "department":[
         {
            "id":"stu2",
            "name":"Sam",
            "height":5,
            "weight":2,
            "bmi":1,
            "type":"Skinny"
         },
         {
            "id":"stu3",
            "name":"REd",
            "height":0,
            "weight":0,
            "bmi":0,
            "type":""
         }
      ]
   }
]

let total = 0
for (let i = 0; i < myObject.length; i  ) { 
  total  = myObject[i].department_count
}

console.log(total);

  • Related