"过滤和QUOT; JSON独特的钥匙,并得到所有相关的值 [英] "Filter" JSON for unique key and get all related values

查看:138
本文介绍了"过滤和QUOT; JSON独特的钥匙,并得到所有相关的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是找到所有与一组可能的相关值的最佳方式。

  VAR表= [
    {组:一,东东:新},
    {组:一,东东:老},
    {组:B的东西:newOld},
    {组:B的东西:旧},
    {组:C的东西:新},
    {组:C的东西:旧},
    {组:C的东西:newOld},
];

我要填充包含独特的值的下拉列表。并选择我想用所有相关的的东西作进一步处理。同时也希望添加它包含了所有的东西 A组。例如:

 所有的选择 - >新,老,newOld
                一个 - >新旧
                b - > newOld,老
                C - >新,老,newOld


解决方案

短期和precise,没有全阵列中的每个时间查找条目的开销:

  VAR组= {所有:{}};
table.forEach(函数(){
    如果(!组[a.group]){组[a.group] = {}; }
    组[a.group] [a.stuff] =组[所有] [a.stuff] = 1;
});

在对象列表中的东西,这样你就没有重复的条目(这就是为什么,而多余的 = 1 )。但是你可以很容易地扩展它来算重复:

  table.forEach(函数(){
    如果(!组[a.group]){组[a.group] = {}; }
    VAR东西=组[所有] [a.stuff];
    组[所有] [a.stuff] =!东西? 1:++的东西;
    东西=组[a.group] [a.stuff];
    组[a.group] [a.stuff] =!东西? 1:++的东西;
});

结果将如下所示:

  //组拥有所有组元素和自己的东西值
组= {全:{新:2,老:3,newOld:2},
           一:{新:1,旧:1},
           B:{newOld:1,老:1},
           C:{新:1,老:1,newOld:1}
         }

要检索组的值,简单地说:

  VAR组名=A; //任何你需要的组
Object.keys(集团[群组名称]);
//会给你:
[新旧]

演示

得留意支持 Object.keys ,当然还有 Array.prototype.forEach

What would be the best way to find all the possible related values with a group.

var table = [
    {group:"a", stuff:"new"},
    {group:"a", stuff:"old"},
    {group:"b", stuff:"newOld"},
    {group:"b", stuff:"old"},
    {group:"c", stuff:"new"},
    {group:"c", stuff:"old"},
    {group:"c", stuff:"newOld"},
];

I want populate a Dropdown containing Unique group values. and on selection I want to use all related stuff for further processing. and also want to add a group which contains all stuff. for example

on selection of all -> new, old, newOld
                a -> new, old
                b -> newOld, old
                c -> new, old, newOld

解决方案

short and precise, without the overhead of looking up the entry each time in the whole array:

var groups = {all:{}};
table.forEach(function(a){
    if (!groups[a.group]){ groups[a.group] = {}; }
    groups[a.group][a.stuff] = groups["all"][a.stuff] = 1;
});

List the stuff in an object, thus you have no duplicate entries (that's why the rather redundant =1). But you could easily extend it to count the duplicates:

table.forEach(function(a){
    if (!groups[a.group]){ groups[a.group] = {}; }
    var stuff = groups["all"][a.stuff];
    groups["all"][a.stuff] = !stuff ? 1 : ++stuff;
    stuff = groups[a.group][a.stuff];
    groups[a.group][a.stuff] = !stuff ? 1 : ++stuff;
});

The result will look like the following:

// "groups" holds all group elements and their stuff values
groups = { "all": {"new":2,"old":3,"newOld":2},
           "a" : {"new":1,"old":1},
           "b" : {"newOld":1,"old":1},
           "c" : {"new":1,"old":1,"newOld":1}
         }

To retrieve the values of a group, simply say:

var groupname = "a"; // whatever group you need
Object.keys(groups[groupname]);
// will give you:
["new","old"]

Demo

Gotta watch out for support of Object.keys and Array.prototype.forEach of course.

这篇关于"过滤和QUOT; JSON独特的钥匙,并得到所有相关的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆