0

我有一个 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"...
        },...
]

我想将其转换为以下格式以便于处理(基本上按公司别名和日期分组)

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"
        }]
},...
]

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

提前致谢

4

2 回答 2

0

使用下面的代码片段获得所需的结果

  1. 通过...分组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>

于 2018-10-04T05:12:52.747 回答
0

下面的代码正在工作。

var inputArray = [
    {
        "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"...
    }
]

var resultArray = groupByMultiple(inputArray ,function(item)
        {
        return [item.companyAlias, item.Date];
});

function groupByMultiple( array , f )
        {
          var groups = {};
          array.forEach( function( o )
          {
            var group = JSON.stringify( f(o) );
            groups[group] = groups[group] || [];
            groups[group].push( o );  
          });
          return Object.keys(groups).map( function( group )
          {
            return groups[group]; 
          })
        }
于 2018-10-12T09:28:47.847 回答