【问题标题】:Spliting each value in Javascript array by list of different values in sequence & then group them按顺序按不同值列表拆分Javascript数组中的每个值,然后对它们进行分组
【发布时间】:2015-03-25 13:13:15
【问题描述】:

我有一个 Javascript 数组。我想根据某个值拆分数组中的每个值,然后对它们进行分组。

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

  1. .& 首先由 .& 拆分
  2. .[All] 如果找不到.&,则根据.[All] 拆分
  3. . 如果 .&.[All] 未找到基于 . 的拆分

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

我能够做到这一切,这里有一个 fiddle 的例子。

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

  1. .&
  2. .[All].UNKNOWNMEMBER --- 新增条件
  3. .[All]
  4. .

我附加的小提琴显示以下输出:

{ [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],如下图所示,我将得到所需的结果。

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

【问题讨论】:

  • 您好 - 我们需要更广泛地了解问题。到目前为止,似乎应该对原始数据进行适当的标准化。
  • @moonwave99:我已经更新了这个问题。我希望它有所帮助。如果您有任何具体问题,请告诉我

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


【解决方案1】:

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

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/

【讨论】:

    【解决方案2】:

    排序和映射

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

    来源:

    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] }
    

    【讨论】:

      猜你喜欢
      • 2018-01-10
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 2018-01-28
      • 2021-08-04
      • 2021-06-02
      相关资源
      最近更新 更多