1

I have an array of grouped objects, but I'm unable to iterate through and achieve the desired result.

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

]

I want to eliminate all possible duplicates and group all similar objects as follows

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

I have tried

Object.entries() 

but it returned

[obeject: object]

I have also tried

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

But I get undefined

Gbless Sylva
  • 75
  • 1
  • 1
  • 7
  • 1
    this is gonna be a bit tricky as `{foo: 'bar'} === {foo: 'bar'}` will evaluate to false (try running that in console to see. it's doable however. for a little more info on that and a possible appraoch, check out: https://stackoverflow.com/questions/11704971/why-are-two-identical-objects-not-equal-to-each-other#:~:text=Objects%20are%20not%20compared%20by,values%20in%20the%20same%20order.&text=Objects%20are%20sometimes%20called%20reference,them%20from%20JavaScript's%20primitive%20types. – HolyMoly Jun 11 '21 at 02:09
  • How does the code decided which is a duplicate entry? Because in the first group under first entry, that is ` 000000010`, though the `userID` is same as the Entry ID itself, both objects have unique values. so how does the code decide which is entry is to be deleted? – Raky Jun 11 '21 at 02:11
  • Also there is a syntax error in your `json` data. Please correct the missing `colons` against few keys such as `played`. – Raky Jun 11 '21 at 02:16
  • The code is grouped from a previous result. each user-id `00000010` had at least 7 entries. With the help of the answer by metakungfu here https://stackoverflow.com/questions/40774697/how-to-group-an-array-of-objects-by-key/40774906#40774906 I was able to group them into the same IDs. – Gbless Sylva Jun 11 '21 at 02:20
  • @Gbless Sylva, But Tell me how does your code decide which is a **Duplicate** Entry. The values are Unique. Also, the format of your **json** data is not correct. Also, do you want to delete the `key` value of `date`? Please look at the Answer given below. – Raky Jun 11 '21 at 02:52
  • @Gbless Sylva, Please vote for the solution that has worked for you. – Raky Jun 11 '21 at 02:58
  • I think talk about removing duplicates is incorrect. What you want to do is merge the values so that the values of played and lost are summed across entries with the same userId. – Paul Rooney Jun 11 '21 at 03:35

2 Answers2

1

If you are looking to sum the played and lost fields you should use reduce to merge the objects, summing the required fields. Then convert the array of entries back into an object.

Try this

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

Node prints the following

{
  '000000010': { userId: '000000010', played: 25, lost: 5 },
  '000000020': { userId: '000000020', played: 26, lost: 4 }
}
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
0

I have corrected the json data format and made this code. The following code does not delete the the key date. Please do tell me if this works for you.

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>
Raky
  • 625
  • 5
  • 19