如何将键从对象推送到数组?

发布于 2024-11-04 20:34:07 字数 299 浏览 0 评论 0原文

假设我有以下对象 obj

obj = {
    'key1' : ['1','2','3'],
    'key2' : ['1','2','9'],
    'key3' : ['1','3','5']
}

如何将 obj 转换为如下所示的两个数组?

allOfTheKeys = ['key1','key2','key3']

allOfTheArrays = ['1','2','3','5','9']

Suppose I have the following object obj:

obj = {
    'key1' : ['1','2','3'],
    'key2' : ['1','2','9'],
    'key3' : ['1','3','5']
}

How can I transform obj into two arrays that look like the following?

allOfTheKeys = ['key1','key2','key3']

allOfTheArrays = ['1','2','3','5','9']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞取 2024-11-11 20:34:07

实际上

allKeys = [];
allElems = [];

for(var k in obj){
   allKeys.push(k);
   for(var e in obj[k]){
      allElem.push(e)
   }
}

,在 jQuery 中,您可以使用 each() 更简洁地做到这一点(警告,这不是经过测试的代码):

jQuery.each(obj,function(key){
    allKeys.push(key); 
    jQuery.each(obj[key],function(elem){
        allElems.push(elem);
    }
});

好的,您不想重复,请添加

if(!(elem in allElems)) allElems.push(elem);

Something like

allKeys = [];
allElems = [];

for(var k in obj){
   allKeys.push(k);
   for(var e in obj[k]){
      allElem.push(e)
   }
}

Actually, in jQuery you can do it more concisely using each() (warning, this isn't tested code):

jQuery.each(obj,function(key){
    allKeys.push(key); 
    jQuery.each(obj[key],function(elem){
        allElems.push(elem);
    }
});

Okay, you don't want repeats, add in

if(!(elem in allElems)) allElems.push(elem);
勿忘初心 2024-11-11 20:34:07

正如我所看到的,其他答案返回重复的值。这里有解决方案(已测试):

var allOfTheKeys = [], allOfTheArrays = [], nonRepeatedElems = {};
for(var key in obj){
   allOfTheKeys.push(key);
   for(var i=0; i< obj[key].length; i++)
      nonRepeatedElems[obj[key][i]] = true;
}
for(var e in nonRepeatedElems )
   allOfTheArrays.push(e);

如果有人想知道 nonRepeatedElems 是什么,它是数组值的哈希表,其键是数组元素值。所以我没有得到重复的元素。

如果您希望对值进行排序,只需最后调用 allOfTheArrays.sort(); 即可。

编辑:@float,这里有一个更容易理解的解决方案:

var allOfTheKeys = [], allOfTheArrays = [];
for(var key in obj){
   allOfTheKeys.push(key);
   for(var i=0; i< obj[key].length; i++){
      var arrayElem = obj[key][i];
      if(!$.inArray(arrayElem, allOfTheArrays)) //Add to the array if it doesn't exist yet
         allOfTheArrays.push(arrayElem);
   }
}

As I saw, other answers return repeated values. Here you have the solution (tested):

var allOfTheKeys = [], allOfTheArrays = [], nonRepeatedElems = {};
for(var key in obj){
   allOfTheKeys.push(key);
   for(var i=0; i< obj[key].length; i++)
      nonRepeatedElems[obj[key][i]] = true;
}
for(var e in nonRepeatedElems )
   allOfTheArrays.push(e);

If someone's wondering what nonRepeatedElems is, it's a hash table for the array values, whose key is the array element value. So I don't get repeated elements.

If you want your values to be ordered, just call allOfTheArrays.sort(); in the end.

EDIT: @float, Here you have a more understandable solution:

var allOfTheKeys = [], allOfTheArrays = [];
for(var key in obj){
   allOfTheKeys.push(key);
   for(var i=0; i< obj[key].length; i++){
      var arrayElem = obj[key][i];
      if(!$.inArray(arrayElem, allOfTheArrays)) //Add to the array if it doesn't exist yet
         allOfTheArrays.push(arrayElem);
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文