var arr = [
{
rq: '2020-01-01',
text: '111'
},
{
rq: '2020-01-01',
text: '222'
},
{
rq: '2020-01-02',
text: '333'
},
{
rq: '2020-01-04',
text: '444'
},
{
rq: '2020-01-04',
text: '555'
},
]
console.log('分组前', arr)
arr = mapLoction(arr,'rq')
console.log('分组后', arr)
function mapLoction(arr,name) {
var newArr = [];
arr.forEach(function(oldData, i) {
var index = -1;
var alreadyExists = newArr.some(function(newData, j) {
if (oldData[name] === newData[name]) {
index = j;
return true;
}
});
if (!alreadyExists) {
newArr.push({
[name]: oldData[name],
res: [oldData]
});
} else {
newArr[index].res.push(oldData);
}
});
return newArr;
};
js按照字段分组
最新推荐文章于 2024-07-24 22:50:18 发布