简体   繁体   English

从JSON数组中查找和删除JSON对象

[英]Find and delete a json object from JSON array

I have a JSON Object something like this below : 我有一个像下面这样的JSON对象:

   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. 现在,我编写了一个从Json数组删除Json对象的函数。

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. 上述功能的唯一问题是,我需要传递JSON数组所属的键的名称。

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 . 基本上,首先,我想找到该JSON数组的键的名称,当我获得键名称时,将其传递给清洁函数 But I don't know how to find the name of the key of the JSON Array. 但是我不知道如何找到JSON数组的键的名称。

With the given structure, you need to iterate over the keys of the object and then over the arrays inside. 在给定的结构,你需要遍历然后在里面的数组对象和按键。

 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 }] }, cleaner = function (object, socket_id) { Object.keys(object).some(function (k) { return object[k].some(function (a, i, aa) { if (a.socket_id === socket_id) { aa.splice(i, 1); return true; } }); }); }; cleaner(users, 'PRQ2czNZuvatsy8cAAAD'); console.log(users); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

More than one to delete approach with Array#filter . 使用Array#filter可以删除多个方法。

 var users = { 'abc@xyz.in': [ { socket_id: '4MIPKfkcitCV9xp6AAAA', community_id: 8 }, { socket_id: '4MIPKfkcitCV9xp6AAAA', community_id: 9 } ] } , cleaner = function (object, socket_id) { Object.keys(object).forEach(function (k) { var temp = object[k].filter(function (a) { return a.socket_id !== socket_id; }); if (object[k].length !== temp.length) { object[k] = temp; } }); }; cleaner(users, '4MIPKfkcitCV9xp6AAAA'); console.log(users); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

try this (replace this with your for loop): 尝试一下(用for循环替换):

for(var i in users)
{
     name = i;
     array = users[i]
}

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

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