简体   繁体   中英

Add fields from array of JSON objects based on specific field

I have an array of objects ...

[  
   {  
      "matchID":"-LP0LKl_nR4VQf6Gxwz8",
      "playerId":"YvtwVY1gsZSzI7ZQzyDTZbSwdLF3",
      "points":"11",
      "id":"-LP1WvT4eN1L7BLbyhJt"
   },
   {  
      "matchID":"-LP0LKl_nR4VQf6Gxwz8",
      "playerId":"YabcVY1gsZSzI7ZQzyDTZbSwdLF6",
      "points":"23",
      "id":"-TP1WvT4eN1L7GeYyhJt"
   },
   {  
      "matchID":"-DF0LKl_nR4VQf6Gxwz7",
      "playerId":"YabcVY1gsZSzI7ZQzyDTZbSwdLF6",
      "points":"12",
      "id":"-GH1WvT4eN1L7GeYyhJt"
   },
   {  
      "matchID":"-DF0LKl_nR4VQf6Gxwz7",
      "playerId":"YvtwVY1gsZSzI7ZQzyDTZbSwdLF3",
      "points":"6",
      "id":"-XZ1WvR2eN1L7GeYyhJt"
   }
]

I want to loop through this array and create a new array that is a basically just each playerId and their total points for all matches.

So after cycling through the above, the array would look like ..

 [{"palyerId": "YabcVY1gsZSzI7ZQzyDTZbSwdLF6", "points": "35"},{"palyerId": "YvtwVY1gsZSzI7ZQzyDTZbSwdLF3", "points": "17"}]

A non reduce version with a for loop looks like the following (again, the central idea is to group by playerId ):

 let data = [{ "matchID": "-LP0LKl_nR4VQf6Gxwz8", "playerId": "YvtwVY1gsZSzI7ZQzyDTZbSwdLF3", "points": "11", "id": "-LP1WvT4eN1L7BLbyhJt" }, { "matchID": "-LP0LKl_nR4VQf6Gxwz8", "playerId": "YabcVY1gsZSzI7ZQzyDTZbSwdLF6", "points": "23", "id": "-TP1WvT4eN1L7GeYyhJt" }, { "matchID": "-DF0LKl_nR4VQf6Gxwz7", "playerId": "YabcVY1gsZSzI7ZQzyDTZbSwdLF6", "points": "12", "id": "-GH1WvT4eN1L7GeYyhJt" }, { "matchID": "-DF0LKl_nR4VQf6Gxwz7", "playerId": "YvtwVY1gsZSzI7ZQzyDTZbSwdLF3", "points": "6", "id": "-XZ1WvR2eN1L7GeYyhJt" } ]; var res = {}; for (let d of data) { if (d.playerId in res) res[d.playerId].points += parseInt(d.points); else res[d.playerId] = {playerId: d.playerId, points: parseInt(d.points)}; } console.log(Object.values(res)); 

You can use reduce() to loop thru the array. Use new Map() to group the array. And use spread operator to convert the map object into an array.

 var arr = [{"matchID":"-LP0LKl_nR4VQf6Gxwz8","palyerId":"YvtwVY1gsZSzI7ZQzyDTZbSwdLF3","points":"11","id":"-LP1WvT4eN1L7BLbyhJt"},{"matchID":"-LP0LKl_nR4VQf6Gxwz8","palyerId":"YabcVY1gsZSzI7ZQzyDTZbSwdLF6","points":"23","id":"-TP1WvT4eN1L7GeYyhJt"},{"matchID":"-DF0LKl_nR4VQf6Gxwz7","palyerId":"YabcVY1gsZSzI7ZQzyDTZbSwdLF6","points":"12","id":"-GH1WvT4eN1L7GeYyhJt"},{"matchID":"-DF0LKl_nR4VQf6Gxwz7","palyerId":"YvtwVY1gsZSzI7ZQzyDTZbSwdLF3","points":"6","id":"-XZ1WvR2eN1L7GeYyhJt"}] var result = [...arr.reduce((c, v) => { if (!c.has(v.palyerId)) c.set(v.palyerId, {"palyerId": v.palyerId,"points": 0}); c.get(v.palyerId).points += +v.points; return c; }, new Map()).values()]; console.log(result); 


Or you can reduce() the array into an object using the palyerId as the key. Use Object.values() to convert the object into an array.

 var arr = [{"matchID":"-LP0LKl_nR4VQf6Gxwz8","palyerId":"YvtwVY1gsZSzI7ZQzyDTZbSwdLF3","points":"11","id":"-LP1WvT4eN1L7BLbyhJt"},{"matchID":"-LP0LKl_nR4VQf6Gxwz8","palyerId":"YabcVY1gsZSzI7ZQzyDTZbSwdLF6","points":"23","id":"-TP1WvT4eN1L7GeYyhJt"},{"matchID":"-DF0LKl_nR4VQf6Gxwz7","palyerId":"YabcVY1gsZSzI7ZQzyDTZbSwdLF6","points":"12","id":"-GH1WvT4eN1L7GeYyhJt"},{"matchID":"-DF0LKl_nR4VQf6Gxwz7","palyerId":"YvtwVY1gsZSzI7ZQzyDTZbSwdLF3","points":"6","id":"-XZ1WvR2eN1L7GeYyhJt"}] var result = Object.values(arr.reduce((c, {palyerId,points}) => { c[palyerId] = c[palyerId] || {palyerId,points: 0}; c[palyerId].points += +points; return c; }, {})); console.log(result); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM