1

I am getting a json objects from database. What is the Best practice to count number of ClassDef values A , B , C.
In the json object for eg: I have in this json " 2 A and 2 B and 3 c "

My json object:

[{
    "ItemCode": "200500303800356",
    "LastCost": 8,
    "OnHand": 593889,
    "InventoryValue": 4751112,
    "PercOfTotalInventory": 18.294517,
    "CumerativePercTotal": 18.29,
    "ClassDef": "A"
}, {
    "ItemCode": "201600701800197",
    "LastCost": 400,
    "OnHand": 300,
    "InventoryValue": 120000,
    "PercOfTotalInventory": 0.462069,
    "CumerativePercTotal": 75.68,
    "ClassDef": "A"
}, {
    "ItemCode": "200701507000107",
    "LastCost": 75,
    "OnHand": 239,
    "InventoryValue": 17925,
    "PercOfTotalInventory": 0.069022,
    "CumerativePercTotal": 91.75,
    "ClassDef": "B"
}, {
    "ItemCode": "200501303400308",
    "LastCost": 3515,
    "OnHand": 5,
    "InventoryValue": 17575,
    "PercOfTotalInventory": 0.067674,
    "CumerativePercTotal": 91.81,
    "ClassDef": "B"
}, {
    "ItemCode": "200200106701035",
    "LastCost": 80,
    "OnHand": 27,
    "InventoryValue": 2160,
    "PercOfTotalInventory": 0.008317,
    "CumerativePercTotal": 99.28,
    "ClassDef": "C"
}, {
    "ItemCode": "200200902700248",
    "LastCost": 10,
    "OnHand": 213,
    "InventoryValue": 2130,
    "PercOfTotalInventory": 0.008202,
    "CumerativePercTotal": 99.29,
    "ClassDef": "C"
}, {
    "ItemCode": "200601302001093",
    "LastCost": 0.3,
    "OnHand": 6,
    "InventoryValue": 1.8,
    "PercOfTotalInventory": 0.000007,
    "CumerativePercTotal": 100,
    "ClassDef": "C"
}]

Does any have an idea how to do this?

8
  • Which language are you looking to do this in - JS or Java? Commented May 18, 2017 at 8:34
  • which value do you loke to count? just how many occurences or a special value? Commented May 18, 2017 at 8:34
  • Use reduce method Commented May 18, 2017 at 8:34
  • i am trying to do it in js Commented May 18, 2017 at 8:35
  • Not sure to well understand your demand. You want to count the number of ClassDef in your whole array ? Or the number of value that contains ClassDef in a single object ? Commented May 18, 2017 at 8:35

6 Answers 6

1

You could use an object for the result and use ClassDef as key.

var data = [{ ItemCode: "200500303800356", LastCost: 8, OnHand: 593889, InventoryValue: 4751112, PercOfTotalInventory: 18.294517, CumerativePercTotal: 18.29, ClassDef: "A" }, { ItemCode: "201600701800197", LastCost: 400, OnHand: 300, InventoryValue: 120000, PercOfTotalInventory: 0.462069, CumerativePercTotal: 75.68, ClassDef: "A" }, { ItemCode: "200701507000107", LastCost: 75, OnHand: 239, InventoryValue: 17925, PercOfTotalInventory: 0.069022, CumerativePercTotal: 91.75, ClassDef: "B" }, { ItemCode: "200501303400308", LastCost: 3515, OnHand: 5, InventoryValue: 17575, PercOfTotalInventory: 0.067674, CumerativePercTotal: 91.81, ClassDef: "B" }, { ItemCode: "200200106701035", LastCost: 80, OnHand: 27, InventoryValue: 2160, PercOfTotalInventory: 0.008317, CumerativePercTotal: 99.28, ClassDef: "C" }, { ItemCode: "200200902700248", LastCost: 10, OnHand: 213, InventoryValue: 2130, PercOfTotalInventory: 0.008202, CumerativePercTotal: 99.29, ClassDef: "C" }, { ItemCode: "200601302001093", LastCost: 0.3, OnHand: 6, InventoryValue: 1.8, PercOfTotalInventory: 0.000007, CumerativePercTotal: 100, ClassDef: "C" }],
    result = data.reduce(function (r, o) {
        r[o.ClassDef] = (r[o.ClassDef] || 0) + 1;
        return r;
    }, Object.create(null));

console.log(result);

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

Comments

1

Through some clever use of .reduce() and Set this is a piece of cake.

const objArray = [{
    "ItemCode": "200500303800356",
    "LastCost": 8,
    "OnHand": 593889,
    "InventoryValue": 4751112,
    "PercOfTotalInventory": 18.294517,
    "CumerativePercTotal": 18.29,
    "ClassDef": "A"
}, ...];

const classDefSet = objArray.reduce((set, obj) => {
    set.add(obj.ClassDef); // add value to the set. If it already exists nothing will happen
    return set;
}, new Set());

const count = [...classDefSet].length; // convert set to plain array
console.log(count); // 3

Comments

1

Yet another reduce example:

let arr = [{
"ItemCode": "200500303800356",
"LastCost": 8,
"OnHand": 593889,
"InventoryValue": 4751112,
"PercOfTotalInventory": 18.294517,
"CumerativePercTotal": 18.29,
"ClassDef": "A"
}, {
"ItemCode": "201600701800197",
"LastCost": 400,
"OnHand": 300,
"InventoryValue": 120000,
"PercOfTotalInventory": 0.462069,
"CumerativePercTotal": 75.68,
"ClassDef": "A"
 }, {
"ItemCode": "200701507000107",
"LastCost": 75,
"OnHand": 239,
"InventoryValue": 17925,
"PercOfTotalInventory": 0.069022,
"CumerativePercTotal": 91.75,
"ClassDef": "B"
 }, {
"ItemCode": "200501303400308",
"LastCost": 3515,
"OnHand": 5,
"InventoryValue": 17575,
"PercOfTotalInventory": 0.067674,
"CumerativePercTotal": 91.81,
"ClassDef": "B"
 },  {
"ItemCode": "200200106701035",
"LastCost": 80,
"OnHand": 27,
"InventoryValue": 2160,
"PercOfTotalInventory": 0.008317,
"CumerativePercTotal": 99.28,
"ClassDef": "C"
 }, {
"ItemCode": "200200902700248",
"LastCost": 10,
"OnHand": 213,
"InventoryValue": 2130,
"PercOfTotalInventory": 0.008202,
"CumerativePercTotal": 99.29,
"ClassDef": "C"
 },  {
"ItemCode": "200601302001093",
"LastCost": 0.3,
"OnHand": 6,
"InventoryValue": 1.8,
"PercOfTotalInventory": 0.000007,
"CumerativePercTotal": 100,
"ClassDef": "C"
}];

let result = arr.reduce((acc, el)=>{
  if (el.hasOwnProperty("ClassDef")){
    if (!acc.hasOwnProperty(el.ClassDef)){
      acc[el.ClassDef] = 0;
    }
    acc[el.ClassDef]++;
  }
  return acc;
}, {});

console.log(result);

Comments

1

Try this to find how many times

  • ClassDef equals C
  • ClassDef equals C or B

var arr=[{ItemCode:"200500303800356",LastCost:8,OnHand:593889,InventoryValue:4751112,PercOfTotalInventory:18.294517,CumerativePercTotal:18.29,ClassDef:"A"},{ItemCode:"201600701800197",LastCost:400,OnHand:300,InventoryValue:12e4,PercOfTotalInventory:.462069,CumerativePercTotal:75.68,ClassDef:"A"},{ItemCode:"200701507000107",LastCost:75,OnHand:239,InventoryValue:17925,PercOfTotalInventory:.069022,CumerativePercTotal:91.75,ClassDef:"B"},{ItemCode:"200501303400308",LastCost:3515,OnHand:5,InventoryValue:17575,PercOfTotalInventory:.067674,CumerativePercTotal:91.81,ClassDef:"B"},{ItemCode:"200200106701035",LastCost:80,OnHand:27,InventoryValue:2160,PercOfTotalInventory:.008317,CumerativePercTotal:99.28,ClassDef:"C"},{ItemCode:"200200902700248",LastCost:10,OnHand:213,InventoryValue:2130,PercOfTotalInventory:.008202,CumerativePercTotal:99.29,ClassDef:"C"},{ItemCode:"200601302001093",LastCost:.3,OnHand:6,InventoryValue:1.8,PercOfTotalInventory:7e-6,CumerativePercTotal:100,ClassDef:"C"}];

function countOccurences(name, value) {
  var count = 0;
  // If name and values are defined
  if (name && value) {
    // Check either a single value or an array of values
    var values = (value instanceof Array ? value : [value])
    // For each object in array
    arr.forEach(function(v) {
      // If values array contains object[name] value
      if (v[name] && values.indexOf(v[name]) !== -1) {
        // Increment counter
        count++;
      }
    });
  }
  return count;
}

console.log("ClassDef === 'C' :", countOccurences("ClassDef", "C"));
console.log("ClassDef === 'C' or 'B' :", countOccurences("ClassDef", ["C", "B"]));

Comments

0

This may useful for you. you can count the values assigned to the key "ClassDef" as show here the count of the duplicate value assigned to the same key in JSON array

Comments

0

Using your sample json object, try this :

var countArr = [];
for(i = 0; i < obj.length; i++)
{
    var val =  obj[i]["ClassDef"];
    if(countArr[val] != null)
    {
        countArr[val] += 1;
    }
    else
    {
        countArr[val] = 1;
    }

}

this will return you

[A: 2, B: 2, C: 3]

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.