Underscore Js - 多个变量的索引有什么方法吗?
Is there any way in Underscorejs - IndexBy for multiple variables?
我有一个 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 和日期分组)
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"
}]
},...
]
如果有人有想法,请告诉我...
提前致谢
使用下面的代码片段获得所需的结果
- GroupBy
companyAlias
- 使用
_.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>
以下代码有效。
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];
})
}
我有一个 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 和日期分组)
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"
}]
},...
]
如果有人有想法,请告诉我...
提前致谢
使用下面的代码片段获得所需的结果
- GroupBy
companyAlias
- 使用
_.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>
以下代码有效。
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];
})
}