I have a JSON Object something like this below :
var users = {
ross: [
{
socket_id: 'K7XhUcIXAQFkmhK7AAAA',
community_id: 2
},
{
socket_id: 'gWBy0adi2e3KoIWuAAAC',
community_id: 2
},
{
socket_id: 'PRQ2czNZuvatsy8cAAAD',
community_id: 2
},
{
socket_id: 'R-EGVCDc5jWQV50KAAAF',
community_id: 2
}
],
laura: [
{
socket_id: 'VCp2NxY42LMNvOclAAAE',
community_id: 2
},
{
socket_id: 'MDZe6Oe8U4xzmUjxAAAG',
community_id: 2
}
],
john: [
{
socket_id: 'Omn3VQKyuYHm2JNdAAAH',
community_id: 2
}
]
}
Now when socket is disconnected, I want to delete that socket object from that user's array. Now I have written a function to delete the Json Object from Json array.
var cleaner= function(arr, id) {
for (var i = 0; i < users.length; i++) {
var cur = users[i];
if (cur.id === id) {
arr.splice(i, 1);
break;
}
}
};
cleaner(users, socket.id);
The only problem with above funtion is that I need to pass the name of the key to which that JSON Array is of.
Basically, first I want to find the name of the key of that JSON Array and when I will get the key name I will pass it to cleaner function. But I don't know how to find the name of the key of the JSON Array.
cur.id === uniqueIdwhere isidproperty coming from and what isuniqueId?currstill doesn't have theidproperty