如何在 JavaScript 中迭代分组对象

问题描述 投票:0回答:3

我有一组分组对象,但无法迭代并达到所需的结果。

[ 000000010: [
  {
    "userId" : "000000010",
    "played" : 10,
    "lost" : 5,
    "date" :"2019-04-01T00:00:00.000Z" 
    },
  {
    "userId" : "000000010",
    "played": 15,
    "lost" : 0,
    "date" :"2019-04-02T00:00:00.000Z" 
    }, 
],
000000020: [
  {
    "userId" : "000000020",
    "played": 11,
    "lost" : 4,
    "date" :"2019-04-01T00:00:00.000Z" 
    },
  {
    "userId" : "000000020",
    "played": 15,
    "lost" : 0,
    "date" :"2019-04-02T00:00:00.000Z" 
    }, 
]

]

我想消除所有可能的重复项并将所有相似的对象分组如下

    {
    "userId" : "000000010",
    "played": 30,
    "lost" : 5,
    },
  {
    "userId" : "000000020",
    "played": 26,
    "lost" : 6,
    }, 

我已经尝试过了

Object.entries() 

但它又回来了

[obeject: object]

我也尝试过

const allResults = {}
Object.keys(result).forEach(function(key) {
    let chats = result[key].chats;
          allResults[chats] = allResults[chats] ? allResults[chats] + 1 : 1;
  });

但我不确定

javascript arrays javascript-objects
3个回答
1
投票

如果您希望对

played
lost
字段求和,您应该使用
reduce
来合并对象,对所需字段求和。然后将条目数组转换回对象。

试试这个

const inputData = {
   "000000010":[
      {
         "userId":"000000010",
         "played":10,
         "lost":5,
         "date":"2019-04-01T00:00:00.000Z"
      },
      {
         "userId":"000000010",
         "played":15,
         "lost":0,
         "date":"2019-04-02T00:00:00.000Z"
      }
   ],
   "000000020":[
      {
         "userId":"000000020",
         "played":11,
         "lost":4,
         "date":"2019-04-01T00:00:00.000Z"
      },
      {
         "userId":"000000020",
         "played":15,
         "lost":0,
         "date":"2019-04-02T00:00:00.000Z"
      }
   ]
};


const result = Object.entries(inputData).map(([key, values]) => {
    const merged = values.reduce((accum, x) => { 
        accum.played += x.played; 
        accum.lost += x.lost; 
        return accum; 
    }, {"userId": key, "played": 0, "lost": 0});
    return [key, merged];
});

console.log(Object.fromEntries(result));

节点打印以下内容

{
  '000000010': { userId: '000000010', played: 25, lost: 5 },
  '000000020': { userId: '000000020', played: 26, lost: 4 }
}

0
投票

我已经更正了 json 数据格式并制作了这段代码。以下代码不会删除

key
date
。请告诉我这是否适合您。

function removeDuplicates() {

  // Create an array of objects
  var movies = [{
      "000000010": [{
          "userId": "000000010",
          "played": 10,
          "lost": 5,
          "date": "2019-04-01T00:00:00.000Z"
        },
        {
          "userId": "000000010",
          "played": 15,
          "lost": 0,
          "date": "2019-04-02T00:00:00.000Z"
        },
      ]
    },
    {
      "000000020": [{
          "userId": "000000020",
          "played": 11,
          "lost": 4,
          "date": "2019-04-01T00:00:00.000Z"
        },
        {
          "userId": "000000020",
          "played": 15,
          "lost": 0,
          "date": "2019-04-02T00:00:00.000Z"
        },
      ]
    }
  ];

  jsonObject = movies.map(JSON.stringify);
  uniqueSet = new Set(jsonObject);
  uniqueArray = Array.from(uniqueSet).map(JSON.parse);
  console.log(uniqueArray);
}
<p>
  Click on the button to remove the duplicated in the array
</p>

<p>Check the console for the output</p>

<button onclick="removeDuplicates();">
  Click here
</button>


0
投票

如果您想使用分组项中的数据创建一个新数组,您可以这样做:

const groupedData = {
"000000010": [
  {
    userId: "000000010",
    played: 10,
  },
  {
    userId: "000000010",
    played: 15,
  },
],
"000000020": [
  {
    userId: "000000020",
    played: 11,
  },
  {
    userId: "000000020",
    played: 15,
  },
 ],
};

const dataFromGroups  = Object.keys(groupedData).map((e)=>{
 console.log(groupedData[e]);
 return groupedData[e];
})

当然,如果需要,您可以将每个键或每个对象分配给另一个变量。

© www.soinside.com 2019 - 2024. All rights reserved.