多维JavaScript数组组内的数据 [英] group data within a multidimensional javascript array

查看:81
本文介绍了多维JavaScript数组组内的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是充斥着大量的冗余数据,我会通过元素中本身喜欢组合在一起多维数组的JavaScript ...

  //当前的阵列
PGROUP =([日下午,名字]); // [0] [0] = PM0,[0] [1] = NAME0 ... [n]的[0] = PMN,[n]的[1] = namen

我的目标是所有冗余点的,那么组的名称中筛选出进入连接到各自的名字的数组。

我试过的.grep,$ .filter几次失败的组合,并与underscore.js修修补补了一下,找到一个解决方案..但作为然而我想出空。

要解决任何指导,将大大AP preciated。

编辑:

//当前阵列

  PGROUP [    [0]
    0:Whimpenny,瓦尔特
    1:105495​​-005_SMS M&放大器; S选项1年
    ,    [1]
    0:Whimpenny,瓦尔特
    1:105495​​-005_SMS M&放大器; S选项2年
    ,    [2]
    0:Sukumar,人员Prasanna
    1:DISA-JCSS /月的工作人员-SO#203868]

//请求的结果。

  PGROUP [    [0] 0:Whimpenny,瓦尔特1:[105495​​-005_SMS M&放大器; S选项1年,105495​​-005_SMS M&放大器; S选项年2]    [1] 0:Sukumar,人员Prasanna1:DISA-JCSS /月的工作人员-SO#203868]]


解决方案

下面是一个使用罗短跑一个实用的解决方案:

  VAR分组= _.map(_。GROUPBY(PGROUP,0),功能(EL){
    返回[埃尔[0] [0],_.pluck(EL,1)];
});

JSBin


另外,你可以行修改重返以下产生的对象,而不是最终产品阵列,通过的Barmar

 ,返回{名称:EL [0] [0],值:_.pluck(EL,1)};

I have a multidimensional javascript array that's filled with a lot of redundant data that I'd like grouped together by elements within itself...

// current array
pGroup = ([pm, name]); // [0][0] = pm0, [0][1] = name0 ... [n][0] = pmn, [n][1] = namen

My goal is to filter out all of the redundant pm's, then group the names into an array attached to their respective names.

I've tried several failed combinations of $.grep, $.filter, and have tinkered with underscore.js a bit to find a solution.. but as of yet I've come up empty.

Any guidance to a solution would be greatly appreciated.

EDIT:

// Current array

pGroup [

    [0]
    0: "Whimpenny, Walter"
    1: "105495-005_SMS M&S Option Year 1"
    ,

    [1]
    0: "Whimpenny, Walter"
    1: "105495-005_SMS M&S Option Year 2"
    , 

    [2]
    0: "Sukumar, Prasanna"
    1: "DISA-JCSS/Staff Aug-SO #203868" 

]

// requested result

pGroup [

    [0] 0: "Whimpenny, Walter" 1: ["105495-005_SMS M&S Option Year 1", "105495-005_SMS M&S Option Year 2"]

    [1] 0: "Sukumar, Prasanna" 1: ["DISA-JCSS/Staff Aug-SO #203868" ]

]

解决方案

Here is a functional solution using Lo-Dash:

var grouped = _.map(_.groupBy(pGroup, 0), function (el) {
    return [el[0][0], _.pluck(el, 1)];
});

JSBin


Alternatively, you could change the return line to the following to yield objects instead of arrays in the final product, as suggested by Barmar:

return { name: el[0][0], values: _.pluck(el, 1) };

这篇关于多维JavaScript数组组内的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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