简体   繁体   English

检查键是否存在于数组中并替换其值

[英]Check if key exists in an array and replace its value

CLIENT CODE 客户代码

I am sending an id of a client from the client side using 我正在使用从客户端发送客户端的ID

var socket = io.connect('http://localhost:8080?token='+userID);

and getting it on the server in my io.on('connection') function using 并使用io.on('connection')函数在服务器上获取它

SERVER CODE 服务器代码

let token = socket.handshake.query.token;

so now I have the userID stored in the variable token . 所以现在我将userID存储在变量令牌中

Also I am getting the connected socket id and storing it like 我也得到了连接的套接字ID并将其存储为

var id = socket.id;

Then I am storing the userID and the socketID in an array as key value pair. 然后,我将用户ID套接字ID作为键值对存储在数组中。

var item = {};
item[token] = id;

This is an array I am pushing it into(the array is declared outside the on connection function) 这是我正在将其推入的数组(该数组在on连接函数外部声明)

keyPairArray.push(item);

So I am getting an array like this when two people are connected. 所以当两个人连接在一起时,我得到的是这样的数组。

[ { '10': 'HvlunYNFCcF5vK0-AAAA' },
  { '1456': '17XF7mbh4vYr2WUSAAAB' } ]

But when any user reloads the page the array gets like 但是,当任何用户重新加载页面时,数组都会像

[ { '10': 'HvlunYNFCcF5vK0-AAAA' },
  { '1456': '17XF7mbh4vYr2WUSAAAB' },
  { '1456': 'tIhvkxFbSSAQEckWAAAC' } ]

Same user Id but different socket id's . 相同的用户ID,但不同的套接字ID

The socket id changes every time the user reloads the page or opens the page in another tab and the same UserID is appended into the array with a different socket id . 每次用户重新加载页面或在另一个选项卡中打开页面时, 套接字ID都会更改,并且同一UserID将使用不同的套接字ID追加到数组中。

I don't want to insert the same user again. 我不想再次插入同一用户。 If the user reloads the page, I want it to check the userId which is the key in the array and replace the old socket id with the newly generated one. 如果用户重新加载页面,我希望它检查数组中的键userId并将旧的套接字ID替换为新生成的套接字ID

This is the code that I tried with no luck 这是我没有运气尝试的代码

var checkKeyPair = keyPairArray.some(function(item){
    return item[token] === id;
});

if(checkKeyPair){
    console.log("Hello");
}else{
    keyPairArray.push(item);
    console.log(keyPairArray);
}

Try this (if ES6 is not a problem): 尝试以下操作(如果ES6没问题):

let token = socket.handshake.query.token;
var id = socket.id;
var item = { [token]: id };
var index = keyPairArray.findIndex(it => Object.keys(it)[0] === token);
if (index === -1) {
    keyPairArray.push(item);
} else {
    keyPairArray[index] = item;
}

You can combine Array#some with Object.keys to check if there is an user with given userId, something like that: 您可以将Array#someObject.keys结合使用,以检查是否存在具有给定userId的用户,如下所示:

 const users = [{ '10': 'HvlunYNFCcF5vK0-AAAA' }, { '1456': '17XF7mbh4vYr2WUSAAAB' } ]; function checkIfUserExists(userId) { return users.some((user) => Object.keys(user).indexOf(userId) > -1) } console.log(checkIfUserExists('10')); console.log(checkIfUserExists('11')); 

 var users = [{ '10': 'HvlunYNFCcF5vK0-AAAA' }, { '1456': '17XF7mbh4vYr2WUSAAAB' } ]; function saveUser(newUserId) { users.forEach(function(key) { if (Object.keys(key)[0] == newUserId) console.log('user exists'); else { var item = {}; item[newUserId] = 'fsdfsdgdfsgdfhgf55665hftgh56'; users.push(item); } }); } saveUser('1456'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM