0

I have a dictionary of arrays as follow:

var usersTemp = {11: [ {"active": true, "email": "[email protected]", "userid": 2, } ],
            12:  [ {"active": true, "email": "[email protected]", "userid": 1, },
                   {"active": true, "email": "[email protected]", "userid": 2, } ], }

I need to merge the elements that have the same email address or userid and get something similar to this:

{ 1: {"active": true, "email": "[email protected]", "roles": [12]} ,
  2: {"active": true, "email": "[email protected]", "roles": [11 , 12]}, }

This is my try:

var mergedUsersTemp = {};
for (var role in usersTemp) {
    for (var user in usersTemp[role]) {
        if(!mergedUsersTemp[user]){
            const i = JSON.parse(JSON.stringify(usersTemp[role][user]));
            console.log(JSON.stringify(i))
            mergedUsersTemp[user] = {"active": i["active"], "email": i["email"], "id": i["id"], "roles": []};
            mergedUsersTemp[user]["roles"] = [];
        }
        mergedUsersTemp[user]["roles"].push(role);
    }
}

But the problem is deep copy in javascript and it's returning the same value for the user info. How can I fix it?

7
  • whats your current output? Commented Mar 10, 2017 at 16:04
  • @Jonasw I've replicated OP's current code in this JSFiddle. Commented Mar 10, 2017 at 16:06
  • user is the index in the array, not the user ID. Try var uid = usersTemp[role][user]['userid']; and use that in if(!mergedUsersTemp[uid]) and so on. This seems to give the result you want. Commented Mar 10, 2017 at 16:07
  • Don't use for...in... for an array: Why is using “for…in” with array iteration a bad idea?. Not a problem here, but don't let it become a habit :) Commented Mar 10, 2017 at 16:13
  • And what's with all the JSON.parse(JSON.stringify(...)) lately? O.o Commented Mar 10, 2017 at 16:14

1 Answer 1

1

Instead of for...in loop its easier to use Object.keys() and forEach loop. Then you just need to first check if userid already exists in object and set its value or add to roles and change active.

var usersTemp = {
  11: [{
    "active": true,
    "email": "[email protected]",
    "userid": 2,
  }],
  12: [{
    "active": true,
    "email": "[email protected]",
    "userid": 1,
  }, {
    "active": true,
    "email": "[email protected]",
    "userid": 2,
  }],
}

var result = {}
Object.keys(usersTemp).forEach(function(i) {
  usersTemp[i].forEach(function(j) {
    if (!result[j.userid]) {
      result[j.userid] = {
        active: j.active,
        email: j.email,
        roles: []
      }
    }
    result[j.userid].roles.push(i)
    result[j.userid].active = j.active
  })
})

console.log(result)

Sign up to request clarification or add additional context in comments.

Comments

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.