Home > Software engineering >  How to combine same entry in multiple lists in json response
How to combine same entry in multiple lists in json response

Time:10-28

I have a quite complex json response which I have to combine, but im totally stuck. I didnt find a smart and effective way yet to detect all votedpositive matches of all users.

  participants =  {
      "data": [
        {
          "userId": 2,
          "votedPositive": [
            {
              "userId": 1
            }
          ]
        },
        {
          "userId": 3,
          "votedPositive": [
            {
              "userId": 5
            }
          ]
        },
        {
          "userId": 4,
          "votedPositive": []
        },
        {
          "userId": 5,
          "votedPositive": [
            {
              "userId": 2,
            },
            {
              "userId": 3
            }
          ]
        },
        {
          "userId": 6,
          "votedPositive": []
        },
        {
          "userId": 7,
          "votedPositive": []
        },
        {
          "userId": 8,
          "votedPositive": []
        },
        {
          "userId": 9,
          "votedPositive": []
        },
        {
          "userId": 10,
          "votedPositive": []
        },
        {
          "userId": 11,
          "votedPositive": []
        },
        {
          "userId": 12,
          "votedPositive": []
        },
        {
          "userId": 1,
          "votedPositive": [
            {
              "userId": 2
            }
          ]
        }
      ]
    }

The output im expecting is a list or array with all the matching pairs. in the above example I would expect to get the information that userId 2 who voted userId 1 has a match because UserId 1 voted userId 2. Same goes for userId 3 and userId 5.

EDIT: This is what I have so far:

var participantsList = [];
  for (var index = 0; index < participants.length; index  ) {
    for (var i = 0; i < participants[index]["votedPositive"].length; i  ) {
      participantsList.push([participants[index]["userId"], participants[index]["votedPositive"][i]["userId"]])
    }
  }
  
for (var index = 0; index < participantsList.length; index  ) {
    participantsList[index].sort(function (a, b) {
      return a - b;
    });
  }

I created a list where im iterating based on the length of all the user ids and im pushing the userid and the voted id. After that I sorted the list so I can see duplicates now. Im not sure if it is working until here. The next step would be to filter out all duplicate combinations in another list, thats where im stuck right now.

The result I have right now is this:

[ [ 1, 2 ], [ 3, 5 ], [ 2, 5 ], [ 3, 5 ], [ 1, 2 ] ]

CodePudding user response:

Create an object whose keys are the voters and values are a set of the users they voted for. Then you can go through this and find the pairs that voted for each other.

participants = {
  "data": [{
      "userId": 2,
      "votedPositive": [{
        "userId": 1
      }]
    },
    {
      "userId": 3,
      "votedPositive": [{
        "userId": 5
      }]
    },
    {
      "userId": 4,
      "votedPositive": []
    },
    {
      "userId": 5,
      "votedPositive": [{
          "userId": 2,
        },
        {
          "userId": 3
        }
      ]
    },
    {
      "userId": 6,
      "votedPositive": []
    },
    {
      "userId": 7,
      "votedPositive": []
    },
    {
      "userId": 8,
      "votedPositive": []
    },
    {
      "userId": 9,
      "votedPositive": []
    },
    {
      "userId": 10,
      "votedPositive": []
    },
    {
      "userId": 11,
      "votedPositive": []
    },
    {
      "userId": 12,
      "votedPositive": []
    },
    {
      "userId": 1,
      "votedPositive": [{
        "userId": 2
      }]
    }
  ]
}

const votedFor = new Map();

participants.data.forEach(({
  userId,
  votedPositive
}) => {
  if (!votedPositive.length) {
    return;
  }
  if (!votedFor[userId]) {
    votedFor.set(userId, new Set());
  }
  votedPositive.forEach(({
      userId: target
    }) =>
    votedFor.get(userId).add(target)
  );
});

const result = [];

votedFor.forEach((votees, voter) =>
  votees.forEach(votee => {
    if (votedFor.get(votee) && votedFor.get(votee).has(voter)) {
      result.push([voter, votee]);
    }
  })
);

console.log(result);

  • Related