Underscorejs中有什么办法-用于多个变量的IndexBy吗?

时间:2018-10-04 04:45:35

标签: javascript angularjs underscore.js ionic-v1

我有一个JSON对象,如下所示 upComingArray”:

[
        {
            "companyAlias": "ABC",
            "refNo": "ABC0001",
            "Date": "12/09/2018",            
            "Sub": "ABC Subj" ...
        },
        {
            "companyAlias": "ABC",
            "refNo": "ABC0002",
            "Date": "12/09/2018", 
            "Sub": "ABC Subj1" ...
        },
        {
            "companyAlias": "ABC",
            "refNo": "ABC0003",
            "Date": "12/09/2018", 
            "Sub": "ABC Subj3"...
        },
        {
            "companyAlias": "BCD",
            "refNo": "BCD0001",
            "Date": "14/09/2018", 
            "Sub": "BCD Subj"...
        },
       {
            "companyAlias": "BCD",
            "refNo": "BCD0002",
            "Date": "14/09/2018", 
            "Sub": "BCD Subj2"...
        },...
]

我想将其转换为以下格式以便于处理(基本上由companyAlias和Date分组)

upComingArray”:

[
        {
            "companyAlias": "ABC",            
            "Date": "12/09/2018" 
             [ {   
                "refNo": "ABC0001"
               "Sub": "ABC Subj"
               },           
               {
                "refNo": "ABC0002",            
                "Sub": "ABC Subj1"
               },
               {            
               "refNo": "ABC0003",            
                "Sub": "ABC Subj3"
            }]
         },
        {
            "companyAlias": "BCD",
            "Date": "14/09/2018"
             [
             {
             "refNo": "BCD0001",
             "Sub": "BCD Subj"
            },
           {

            "refNo": "BCD0002",            
            "Sub": "BCD Subj2"
        }]
},...
]

如果有人有想法,请告诉我...

预先感谢

2 个答案:

答案 0 :(得分:0)

使用以下代码段获取所需结果

  1. GroupBy companyAlias
  2. 使用_.map

  let arr = [
        {
            "companyAlias": "ABC",
            "refNo": "ABC0001",
            "Date": "12/09/2018",
            "Sub": "ABC Subj"
        },
        {
            "companyAlias": "ABC",
            "refNo": "ABC0002",
            "Date": "12/09/2018",
            "Sub": "ABC Subj1"
        },
        {
            "companyAlias": "ABC",
            "refNo": "ABC0003",
            "Date": "12/09/2018",
            "Sub": "ABC Subj3"
        },
        {
            "companyAlias": "BCD",
            "refNo": "BCD0001",
            "Date": "14/09/2018",
            "Sub": "BCD Subj"
        },
       {
            "companyAlias": "BCD",
            "refNo": "BCD0002",
            "Date": "14/09/2018",
            "Sub": "BCD Subj2"
        }
];


let arry1 = _.groupBy(arr, 'companyAlias' );

let finalArr = _.map(arry1, function(value, key){
  return {
    'companyAlias': key ,
    'Date': value[0].Date ,
    arr: _.map(value, function(arry){ return { 'refNo': arry.refNo, 'Sub': arry.Sub } })
  }
});

console.log('Final Array : ', finalArr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>

答案 1 :(得分:0)

下面的代码有效。

// The premium icon font for Ionic. For more info, please see:
// http://ionicframework.com/docs/ionicons/

@import "ionic.ionicons";