-4

For the given array object

map = [
        {
         "userId":"2345454",
         "socketIds":["asd2324","asdfs3242"]
        },
        {
         "userId":"353453",
         "socketIds":["asdf3456","as234s","sfsdf324"]
        }
      ];

I get a new socket Id and a user Id to be added to the map. How do I search through the array for the user Id. And push the socket Id if it exists to the element, or add a new element with the socket Id and the user Id to the array if no element matches the userId provided?

Hemant Kumar Goyal
  • 132
  • 1
  • 2
  • 9

5 Answers5

1

You can find the index and update if it exists

var userId = "2345454";
var id= "56";
var map = [
        {
         "userId":"2345454",
         "socketIds":["asd2324","asdfs3242"]
        },
        {
         "userId":"353453",
         "socketIds":["asdf3456","as234s","sfsdf324"]
        }
      ];
      var idx = map.map(function(val){ return val.userId}).indexOf(userId);
      if(idx > -1) {
          map[idx].socketIds.push(id);
      } else {
        map.push({userId: userId, socketIds: [id]});
      }
     console.log(map);  
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
0
var user = map.find(function(ele){return ele.id == userId})
if(user) user.socketIds.push(socketId)
Abhilash
  • 196
  • 11
0

Here is what you should try

var map = [
    {
     "userId":"2345454",
     "socketIds":["asd2324","asdfs3242"]
    },
    {
     "userId":"353453",
     "socketIds":["asdf3456","as234s","sfsdf324"]
    }
];
   
function inserOrUpdate(newObj){
  var obj = map.find(function (el) {
   return el.userId==newObj.userId;
  });
  if(obj){
   obj.socketIds = newObj.socketIds;
  }else{
   map.push(newObj);
  }
}
  
inserOrUpdate({
"userId":"2345454",
"socketIds":["aaaaaaa","bbbbbbb"]
});

console.log(JSON.stringify(map));
  
Hari Das
  • 10,145
  • 7
  • 62
  • 59
0

Here is Code that will also help to avoid duplicate values in socketIds as well.

var map = [
    {
     "userId":"2345454",
     "socketIds":["asd2324","asdfs3242"]
    },
    {
     "userId":"353453",
     "socketIds":["asdf3456","as234s","sfsdf324"]
    }
  ];

var idx = map.map(function(val){ return val.userId}).indexOf(paramUserId);
  if(idx > -1) {
        var idxx= map[idx].socketIds.indexOf(paramUserId);

        if(idxx==-1){
            map[idx].socketIds.push(paramSocketId);    
        }
  } else {
    map.push({userId: paramUserId, socketIds: [paramSocketId]});
  }
console.log(map);
Vivek Nerle
  • 304
  • 2
  • 14
0

This will add a new user and socket if it doesn't exist, or just push a new socket value to an existing user if the user exists.

var map = [
        {
         "userId":"2345454",
         "socketIds":["asd2324","asdfs3242"]
        },
        {
         "userId":"353453",
         "socketIds":["asdf3456","as234s","sfsdf324"]
        }
      ];

function addOrUpdate(userArray, userId, socketId) {
  const user = userArray.find(usr =>  usr.userId === userId);
  if(user) {
    user.socketIds.push(socketId);
  } else {
    userArray.push({userId: userId, socketIds: [socketId]});
  }

}
addOrUpdate(map, '2345454', '123888');
addOrUpdate(map, '999999', 'sock1');

console.log(map); 
dashton
  • 2,684
  • 1
  • 18
  • 15