代码之家  ›  专栏  ›  技术社区  ›  AbreQueVoy

计算JSON数组中的值的总和

  •  2
  • AbreQueVoy  · 技术社区  · 5 年前

    有一个这样的数组:

    const data = [
        {
            "name": "Dave",
            "coins": 14,
            "weapons": 2,
            "otherItems": 3,
            "color": "red"
        },
        {
            "name": "Vanessa",
            "coins": 18,
            "weapons": 1,
            "otherItems": 5,
            "color": "blue"
        },
        {
            "name": "Sharon",
            "coins": 9,
            "weapons": 5,
            "otherItems": 1,
            "color": "pink"
        },
        {
            "name": "Walter",
            "coins": 9,
            "weapons": 2,
            "otherItems": 4,
            "color": "white"
        }
    ]
    

    如何计算总和 coins , weapons otherItems 使用ES6功能?(我不喜欢这个:任何简单的方法都是好的。)

    data.reduce((first, last) => first + last) 生成一个链 [object Object][object Object] s

    4 回复  |  直到 5 年前
        1
  •  5
  •   mickl    5 年前

    您必须单独处理每个字段(请注意,当您没有为指定第二个参数时 reduce 它将把第一个数组对象作为种子,并从第二个数组对象开始处理):

    const data = [
        {
            "name": "Dave",
            "coins": 14,
            "weapons": 2,
            "otherItems": 3,
            "color": "red"
        },
        {
            "name": "Vanessa",
            "coins": 18,
            "weapons": 1,
            "otherItems": 5,
            "color": "blue"
        },
        {
            "name": "Sharon",
            "coins": 9,
            "weapons": 5,
            "otherItems": 1,
            "color": "pink"
        },
        {
            "name": "Walter",
            "coins": 9,
            "weapons": 2,
            "otherItems": 4,
            "color": "white"
        }
    ]
    
    let result = data.reduce((a,c)=> ({ 
         coins: a.coins + c.coins, 
         weapons: a.weapons + c.weapons, 
         otherItems: a.otherItems + c.otherItems })
    )
    
    console.log(result);
        2
  •  2
  •   Nina Scholz    5 年前

    您可以为总和获取一组所需的键,并为总和创建一个对象,然后添加所需的值。

    const
        data = [{ name: "Dave", coins: 14, weapons: 2, otherItems: 3, color: "red" }, { name: "Vanessa", coins: 18, weapons: 1, otherItems: 5, color: "blue" }, { name: "Sharon", coins: 9, weapons: 5, otherItems: 1, color: "pink" }, { name: "Walter", coins: 9, weapons: 2, otherItems: 4, color: "white" }],
        keys = ['coins', 'weapons', 'otherItems'],
        sums = data.reduce(
            (r, o) => (keys.forEach(k => r[k] += o[k]), r), 
            Object.fromEntries(keys.map(k => [k, 0]))
        );
    
    console.log(sums);
        3
  •  1
  •   Fullstack Guy    5 年前

    您可以使用 Array.prototype.reduce 为了这个。

    为了使其更加灵活和动态,制作一个 Set 你想要清点的钥匙数量。

    然后逐一检查 钥匙 设置 如果该密钥在 obj ,将其总结为 accumulator reduce回调中的对象:

    const data = [{"name":"Dave","coins":14,"weapons":2,"otherItems":3,"color":"red"},{"name":"Vanessa","coins":18,"weapons":1,"otherItems":5,"color":"blue"},{"name":"Sharon","coins":9,"weapons":5,"otherItems":1,"color":"pink"},{"name":"Walter","coins":9,"weapons":2,"otherItems":4,"color":"white"}]
    
    //Keys to count
    const keys = new Set(["coins", "weapons", "otherItems"]);
    
    const count = data.reduce((acc, obj) => {
      const objKeys = keys.forEach(key => {
        if (obj.hasOwnProperty(key)) {
          acc[key] = (acc[key] || 0) + obj[key];
        }
      });
      return acc;
    }, {});
    console.log(count);
        4
  •  0
  •   Antoni Silvestrovič    5 年前

    你的想法是对的,你需要使用 reduce 方法。问题是,你是在求两个对象的和,而不是它们的属性。你所需要做的就是将代码更改为以下内容(对硬币求和):

    data.reduce((first, last) => first.coins + last.coins, 0)
    

    以下是武器:

    data.reduce((first, last) => first.weapons + last.weapons, 0)