按顺序将不同值的列表拆分为Javascript数组中的每个值;然后将它们分组

时间:2015-03-25 13:13:15

标签: javascript jquery underscore.js handlebars.js lodash

我有一个Javascript数组。我想基于某个值&分割数组中的每个值。然后将它们分组。

以下是按优先级顺序排列的条件/值,我想根据这些条件/值拆分数组中的值。

  1. .&首先按.&
  2. 拆分 如果找不到.[All],则
  3. .&,根据.[All]进行拆分
  4. .如果找不到基于.&
  5. .[All].分组

    然后我需要加入由,组合在一起的所有值。

    我能做到这一切&这是一个fiddle相同的例子。

    问题:现在我想按照下面提到的顺序拆分以下值

    1. .&
    2. .[All].UNKNOWNMEMBER --- 新增条件
    3. .[All]
    4. .
    5. 我附上的小提琴显示以下输出:

      { [Dim1].[Att].&[001], [Dim1].[Att].&[002] } * { [Dim1].[Att].[All].UNKNOWNMEMBER } * { [Dim3].[Att].[All].Children } * { [Dim4].[Att].[All].Children } * { [M1].[One], [M1].[Two] }
      

      我希望将输出更改为

      { [Dim1].[Att].&[001], [Dim1].[Att].&[002] , [Dim1].[Att].[All].UNKNOWNMEMBER } * { [Dim3].[Att].[All].Children } * { [Dim4].[Att].[All].Children } * { [M1].[One], [M1].[Two] }
      

      目前,当我按[Dim1].[Att].&[001]分割.&时,我得到[Dim1].[Att].&,但如果我们将分割操作后的输出更改为[Dim1].[Att],如下图所示,我会得到期望的结果。

      This is how I want to split and group

      我不想使用for循环来获得所需的结果。

2 个答案:

答案 0 :(得分:0)

如果您想加入.[All].UNKNOWNMEMBER组中的.&,那么您可以在groupBy()函数中替换此文字吗? E.g:

var res = _.chain(selectionList)
    .groupBy(function (x) {
    return x.replace("\.\[All\]\.UNKNOWNMEMBER",".&")
            .match(/.+?\.&|.+?\.\[All\]|[^.]+\./i)[0];
})

如果您希望对数组中的元素进行排序(因此在.&之前有UNKOWNMEMBER个元素,您可以将其添加到map()函数中:

    .map(function (x) {

    return "{ " + x.sort().join(", ") + " }";
})

小提琴演示:http://jsfiddle.net/kmbubana/3/

答案 1 :(得分:0)

排序和地图

您可以使用自定义排序获得所需的输出,该排序根据拆分条件创建排名。

来源:

var s = [
    "[Dim1].[Att].&[001]",
    "[Dim1].[Att].[All].UNKNOWNMEMBER",
    "[Dim1].[Att].&[002]",
    "[Dim3].[Att].[All].Children",
    "[Dim4].[Att].[All].Children",
    "[M1].[One]",
    "[M1].[Two]"];

// Sort the items.
s.sort(function(a,b){

  // Give items a rank based on what we find. 1 is high, 10 is low.
  var x = a.indexOf(".&") > -1 ? 0 : 10;
  if (x === 10) x = a.indexOf(".[All].UNKNOWNMEMBER") > -1 ? 1 : 10;
  if (x === 10) x = a.indexOf(".[All]") > -1 ? 2 : 10;
  if (x === 10) x = a.indexOf(".") > -1 ? 3 : 10;

  var y = b.indexOf(".&") > -1 ? 0 : 10;
  if (y === 10) y = b.indexOf(".[All].UNKNOWNMEMBER") > -1 ? 1 : 10;
  if (y === 10) y = b.indexOf(".[All]") > -1 ? 2 : 10;
  if (y === 10) y = b.indexOf(".") > -1 ? 3 : 10;

  // Sort according to rank.
  if (x > y) {
    return 1;
  } else if (x === y) {
    return 0;
  } else {
    return -1;
  }

});


console.log(s);

// Create output for the groups.
var output = "{";
var key = "";

s.map(function(item){

  var thisKey = item.substr(0, item.indexOf(".")-1);

  if (thisKey !== key) {
    if (key !== "") output += "} * ";
    output += "{ ";
    key = thisKey;
  } else {
    output += " , "
  }

  output += item;
});

output += " }";

console.log(output);

输出:

 ["[Dim1].[Att].&[001]", "[Dim1].[Att].&[002]", "[Dim1].[Att].[All].UNKNOWNMEMBER", "[Dim3].[Att].[All].Children", "[Dim4].[Att].[All].Children", "[M1].[One]", "[M1].[Two]"]

{{ [Dim1].[Att].&[001] , [Dim1].[Att].&[002] , [Dim1].[Att].[All].UNKNOWNMEMBER} * { [Dim3].[Att].[All].Children} * { [Dim4].[Att].[All].Children} * { [M1].[One] , [M1].[Two] }