0

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

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: stackoverflow.com/questions/11704971/…. Commented Jun 11, 2021 at 2: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? Commented Jun 11, 2021 at 2:11
  • Also there is a syntax error in your json data. Please correct the missing colons against few keys such as played. Commented Jun 11, 2021 at 2: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 stackoverflow.com/questions/40774697/… I was able to group them into the same IDs. Commented Jun 11, 2021 at 2: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. Commented Jun 11, 2021 at 2:52

2 Answers 2

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 }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Paul Rooney, Your solution is good and interesting. Thanks.
-2

If you want to create a new array with the data from the grouped items you can do it like this:

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

Of course you can asing each key or each object to another variable if you need it, you can check [here in object keys]: https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
@Community Bot that's ridiculous to say and DownVote my post. There are so many repsonses where "further details, such as citations or documentation.."is missing. This act appears to mal

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.