【问题标题】:How to count property different values in json object如何计算json对象中的属性不同值
【发布时间】:2018-12-13 16:24:03
【问题描述】:

我正在从数据库中获取一个 json 对象。计算ClassDefA , B , C 的数量的最佳做法是什么。
例如在 json 对象中:我在这个 json 中有“2 A and 2 B and 3 c”

我的 json 对象:

[{
    "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"
}]

有人知道怎么做吗?

【问题讨论】:

  • 您希望在哪种语言中执行此操作 - JS 或 Java?
  • 您喜欢计算哪个值?到底有多少次出现或一个特殊值?
  • 使用reduce方法
  • 我正在尝试用 js 来做
  • 不确定是否充分了解您的需求。您想计算整个数组中ClassDef 的数量吗?还是单个对象中包含ClassDef 的值的数量?

标签: javascript jquery json ajax


【解决方案1】:

您可以使用对象作为结果并使用ClassDef 作为键。

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

【讨论】:

    【解决方案2】:

    通过巧妙地使用.reduce()Set,这是小菜一碟。

    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
    

    【讨论】:

      【解决方案3】:

      又一个reduce 例子:

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

      【讨论】:

        【解决方案4】:

        试试这个找出多少次

        • ClassDef 等于 C
        • ClassDef 等于 CB

        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"]));

        【讨论】:

          【解决方案5】:

          这可能对您有用。您可以计算分配给键“ClassDef”的值,如下所示the count of the duplicate value assigned to the same key in JSON array

          【讨论】:

            【解决方案6】:

            使用您的示例 json 对象,试试这个:

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

            这会让你回来

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-11-27
              • 2022-01-20
              • 2022-07-03
              • 2019-04-24
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多