Javascript - >添加具有相同名称

时间:2018-03-21 15:21:51

标签: javascript json reactjs

我正在尝试计算每个问题每个玩家得分的总分。

对于每个问题,我都会按以下格式检索数据。

[
  {
    "_id":"5ab24e5e49e0f20a06d73ab7",
    "player":"Kareltje",
    "answers":
    [
      {
        "_id":"5ab227cf07818240934b11a5",
        "player":"Peter",
        "comment":"",
        "points":7,
        "__v":0
      },
      {
        "_id":"5ab24bf3b8494c76fd0bb31a",
        "player":"André",
        "comment":"",
        "points":6,
        "__v":0
      },
      {
        "_id":"5ab24bf7b8494c76fd0bb31b",
        "player":"Maikel",
        "comment":"",
        "points":5,
        "__v":0
      },
      {
        "_id":"5ab24bfab8494c76fd0bb31c",
        "player":"Iebele",
        "comment":"",
        "points":4,
        "__v":0
      },
      {
        "_id":"5ab24bfeb8494c76fd0bb31d",
        "player":"Bettina",
        "comment":"",
        "points":3,
        "__v":0
      },
      {
        "_id":"5ab24c01b8494c76fd0bb31e",
        "player":"Shirley",
        "comment":"",
        "points":2,
        "__v":0
      },
      {
        "_id":"5ab24c04b8494c76fd0bb31f",
        "player":"Suzanne",
        "comment":"",
        "points":1,
        "__v":0
      }
    ],
    "question":1,"__v":0
  },
  {
    "_id":"5ab24fa21e7caa1132720e7a",
    "player":"Maikel",
    "answers":
    [
      {
        "_id":"5ab24c04b8494c76fd0bb31f",
        "player":"Suzanne",
        "comment":"",
        "points":7,
        "__v":0
      },
      {
        "_id":"5ab24bfab8494c76fd0bb31c",
        "player":"Iebele",
        "comment":"",
        "points":6,
        "__v":0
      },
      {
        "_id":"5ab24bf3b8494c76fd0bb31a",
        "player":"André",
        "comment":"",
        "points":5,
        "__v":0
      },
      {
        "_id":"5ab24c01b8494c76fd0bb31e",
        "player":"Shirley",
        "comment":"",
        "points":4,
        "__v":0
      },
      {
        "_id":"5ab24bf7b8494c76fd0bb31b",
        "player":"Maikel",
        "comment":"",
        "points":3,
        "__v":0
      },
      {
        "_id":"5ab24bfeb8494c76fd0bb31d",
        "player":"Bettina",
        "comment":"",
        "points":2,
        "__v":0
      },
      {
        "_id":"5ab227cf07818240934b11a5",
        "player":"Peter",
        "comment":"",
        "points":1,
        "__v":0
      }
    ],
    "question":1,"__v":0
  }
]

我希望根据这些数据得到每个玩家的总得分,但我似乎找不到代码,将每个玩家的积分加起来。

结果如下: 彼得:14 安德烈:12 迈克尔:10 Iebele:8

关于如何实现这一目标的任何想法?

我试图通过以下代码获得积分:

var { data, players } = this.state;
var ArrLength = data.length;
console.log(data);
var j;
var x;
for (j = 0; j < ArrLength; j++) {
  let answer = data[j].answers;
  for (x = 0; x < answer.length; x++) {
    console.log(answer[`${x}`].points);
  }
}

这至少向我展示了console.log中每位玩家的积分。但现在添加它们以获得最终结果是我似乎无法弄清楚的。

4 个答案:

答案 0 :(得分:2)

您可以使用Map进行计数并使用数据构建数组。

&#13;
&#13;
var data = [{ answers: [{ _id: "5ab227cf07818240934b11a5", player: "Peter", comment: "", points: 7, __v: 0 }, { _id: "5ab24bf3b8494c76fd0bb31a", player: "André", comment: "", points: 6, __v: 0 }, { _id: "5ab24bf7b8494c76fd0bb31b", player: "Maikel", comment: "", points: 5, __v: 0 }, { _id: "5ab24bfab8494c76fd0bb31c", player: "Iebele", comment: "", points: 4, __v: 0 }], player: "Pieter", question: 1, __v: 0, _id: "5ab24e5e49e0f20a06d73ab7" }, { answers: [{ _id: "5ab227cf07818240935b11a5", player: "Peter", comment: "", points: 7, __v: 0 }, { _id: "5ab24bf3b8494c76fd8bb31a", player: "André", comment: "", points: 6, __v: 0 }, { _id: "5ab24bf7b8494c76fd2bb31b", player: "Maikel", comment: "", points: 5, __v: 0 }, { _id: "5ab24bfab8494c76fd9bb31c", player: "Iebele", comment: "", points: 4, __v: 0 }], player: "Kareltje", question: 1, __v: 0, _id: "5ab24e5e49e0f20b86d73ab7" }],
    score = Array.from(
        data.reduce((m, { answers }) =>
            answers.reduce((n, { player, points }) =>
                n.set(player, (n.get(player) || 0) + points), m), new Map),
        ([player, score]) => ({ player, score })
    );

console.log(score);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 1 :(得分:2)

您可以使用此替代方案。

使用函数reduce和函数forEach

  • reduce积累。
  • forEach循环回答。

var data = [{    answers: [        {            _id: "5ab227cf07818240934b11a5",             player: "Peter",             comment: "",             points: 7,             __v: 0        }        , {            _id: "5ab24bf3b8494c76fd0bb31a",             player: "André",             comment: "",             points: 6,             __v: 0        }        , {            _id: "5ab24bf7b8494c76fd0bb31b",             player: "Maikel",             comment: "",             points: 5,             __v: 0        }        , {            _id: "5ab24bfab8494c76fd0bb31c",             player: "Iebele",             comment: "",             points: 4,             __v: 0        }    ],    player: "Pieter",    question: 1,    __v: 0,    _id: "5ab24e5e49e0f20a06d73ab7"},{    answers: [        {            _id: "5ab227cf07818240935b11a5",             player: "Peter",             comment: "",             points: 7,             __v: 0        },         {            _id: "5ab24bf3b8494c76fd8bb31a",             player: "André",             comment: "",             points: 6,             __v: 0        }        , {            _id: "5ab24bf7b8494c76fd2bb31b",             player: "Maikel",             comment: "",             points: 5,             __v: 0        }        ,{            _id: "5ab24bfab8494c76fd9bb31c",             player: "Iebele",             comment: "",             points: 4,             __v: 0        }    ],    player: "Kareltje",    question: 1,    __v: 0,    _id: "5ab24e5e49e0f20b86d73ab7"}];

var result = data.reduce((a, {answers}) => {
  answers.forEach(({player, points}) =>  a[player] = (a[player] || 0) + points);
  return a;
}, {});


console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:1)

您只需使用外部阵列即可。它基本上对points中的每个元素使用相同的密钥,它验证密钥是否已设置(如果不是,设置为零),并对与相同密钥相关的点进行求和。

PS:_id在你的json中不是唯一的。

由于您提供的JSON无效,我使用以下内容:

var arr = [
    {
        answers: [{
                _id: "5ab227cf07818240934b11a5", 
                player: "Peter", 
                comment: "", 
                points: 7, 
                __v: 0
            }, {
                _id: "5ab24bf3b8494c76fd0bb31a", 
                player: "André", 
                comment: "", 
                points: 6, 
                __v: 0
            }, {
                _id: "5ab24bf7b8494c76fd0bb31b", 
                player: "Maikel", 
                comment: "", 
                points: 5, 
                __v: 0
            }, {
                _id: "5ab24bfab8494c76fd0bb31c", 
                player: "Iebele", 
                comment: "", 
                points: 4, 
                __v: 0
            }
        ],
        player: "Pieter",
        question: 1,
        __v: 0,
        _id: "5ab24e5e49e0f20a06d73ab7"
    }, {
        answers: [{
                _id: "5ab227cf07818240935b11a5", 
                player: "Peter", 
                comment: "", 
                points: 7, 
                __v: 0
            }, {
                _id: "5ab24bf3b8494c76fd8bb31a", 
                player: "André", 
                comment: "", 
                points: 6, 
                __v: 0
            }, {
                _id: "5ab24bf7b8494c76fd2bb31b", 
                player: "Maikel", 
                comment: "", 
                points: 5, 
                __v: 0
            }, {
                _id: "5ab24bfab8494c76fd9bb31c", 
                player: "Iebele", 
                comment: "", 
                points: 4, 
                __v: 0
            }
        ],
        player: "Kareltje",
        question: 1,
        __v: 0,
        _id: "5ab24e5e49e0f20b86d73ab7"
    }
]

var points = {};
for (var i in arr) {
    for (var j in arr[i].answers) {
        if (!points[arr[i].answers[j].player]) {
            points[arr[i].answers[j].player] = 0;
        }
        points[arr[i].answers[j].player] += arr[i].answers[j].points;
    }
}

答案 3 :(得分:1)

您可以使用.reduce遍历数组并汇总所有分数:

const data = [{"answers":[{"_id":"5ab227cf07818240934b11a5","player":"Peter","comment":"","points":7,"__v":0},{"_id":"5ab24bf3b8494c76fd0bb31a","player":"André","comment":"","points":6,"__v":0},{"_id":"5ab24bf7b8494c76fd0bb31b","player":"Maikel","comment":"","points":5,"__v":0},{"_id":"5ab24bfab8494c76fd0bb31c","player":"Iebele","comment":"","points":4,"__v":0}],"player":"Pieter","question":1,"__v":0,"_id":"5ab24e5e49e0f20a06d73ab7"},{"answers":[{"_id":"5ab227cf07818240935b11a5","player":"Peter","comment":"","points":7,"__v":0},{"_id":"5ab24bf3b8494c76fd8bb31a","player":"André","comment":"","points":6,"__v":0},{"_id":"5ab24bf7b8494c76fd2bb31b","player":"Maikel","comment":"","points":5,"__v":0},{"_id":"5ab24bfab8494c76fd9bb31c","player":"Iebele","comment":"","points":4,"__v":0}],"player":"Kareltje","question":1,"__v":0,"_id":"5ab24e5e49e0f20b86d73ab7"}];

const scores = data.reduce( (scores, d) => {
  
  d.answers.forEach( answer => {
    scores[answer.player] = (scores[answer.player]||0) + answer.points;
  });

  return scores;
}, {});

console.log(scores);