admin管理员组文章数量:1291084
I hate this array of objects, each object has a date, I want to be able to group these objects into months. Is there a way to convert this,
var data = [
{ date: "2016-08-13",...},
{ date: "2016-07-23",...},
{ date: "2016-08-11",...},
{ date: "2016-08-10",...},
{ date: "2016-07-20",...},
{ date: "2016-07-21",...},
]
into something like this
var data = [
[{ date: "2016-08-13",...},
{ date: "2016-08-11",...},
{ date: "2016-08-10",...}],
[{ date: "2016-07-20",...},
{ date: "2016-07-21",...},
{ date: "2016-07-23",...}[
]
I hate this array of objects, each object has a date, I want to be able to group these objects into months. Is there a way to convert this,
var data = [
{ date: "2016-08-13",...},
{ date: "2016-07-23",...},
{ date: "2016-08-11",...},
{ date: "2016-08-10",...},
{ date: "2016-07-20",...},
{ date: "2016-07-21",...},
]
into something like this
var data = [
[{ date: "2016-08-13",...},
{ date: "2016-08-11",...},
{ date: "2016-08-10",...}],
[{ date: "2016-07-20",...},
{ date: "2016-07-21",...},
{ date: "2016-07-23",...}[
]
Share
Improve this question
asked Nov 14, 2017 at 7:40
relidonrelidon
2,3124 gold badges29 silver badges46 bronze badges
1
- Possible duplicate of What is the most efficient method to groupby on a javascript array of objects? – Andreas Commented Nov 14, 2017 at 7:44
5 Answers
Reset to default 5You could take a part of the string for year and month group in a hash table and take for every group a new array and put this array to the result set.
var data = [{ date: "2016-08-13" }, { date: "2016-07-23" }, { date: "2016-08-11" }, { date: "2016-08-10" }, { date: "2016-07-20" }, { date: "2016-07-21" }],
hash = Object.create(null),
result = [];
data.forEach(function (o) {
var key = o.date.slice(0, 7);
if (!hash[key]) {
hash[key] = [];
result.push(hash[key]);
}
hash[key].push(o);
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can use array#reduce
to group object based on month.
var data = [{ date: "2016-08-13"},{ date: "2016-07-23"},{ date: "2016-08-11"},{ date: "2016-08-10"},{ date: "2016-07-20"},{ date: "2016-07-21"}];
var result = data.reduce((res,obj) => {
let [year, month, day] = obj.date.split('-');
if(res[month])
res[month].push(obj);
else
res[month] = [obj];
return res;
},{});
console.log(Object.values(result));
.as-console-wrapper { max-height: 100% !important; top: 0; }
var data = [{ date: "2016-08-13"},{ date: "2016-07-23"},{ date: "2016-08-11"},{ date: "2016-08-10"},{ date: "2016-07-20"},{ date: "2016-07-21"}];
var result = data.reduce((res,obj) => {
let [year, month, day] = obj.date.split('-');
res[month] = res[month] || [];
res[month].push(obj);
return res;
},{});
console.log(Object.values(result));
Group by Month:
var groupedData = [];
for(i=0;i<12;i++)
groupedData.push([]);
for(var index = 0;index<data.length;index++){
var date = new Date(data[index].date);
groupedData[date.getMonth()] = data[index];
}
var data = [
{ date: "2016-08-13"},
{ date: "2016-07-23"},
{ date: "2016-08-11"},
{ date: "2016-08-10"},
{ date: "2016-07-20"},
{ date: "2016-07-21"},
];
data.sort(function(a, b) {
var aDate = new Date(a.date);
var bDate = new Date(b.date);
return bDate - aDate;
});
console.log(data);
var data = [
{ date: "2016-08-13"},
{ date: "2016-07-23"},
{ date: "2016-08-11"},
{ date: "2016-08-10"},
{ date: "2016-07-20"},
{ date: "2016-07-21"}
];
var monthDateIndex = {
"01": "Jan",
"02" : "Feb",
"03" : "mar",
"04" : "april",
"05" : "may",
"06" : "june",
"07" : "july",
"08" : "august",
"09" : "sept",
"10" : "oct",
"11" : "nov",
"12" : "dec"
}
let newDataObj = [];
Object.keys(monthDateIndex).map(monthNum => {
let monthObj = [];
data.map(dateObj => {
if(monthNum == dateObj.date.split("-")[1]) {
monthObj.push(dateObj);
}
});
if(monthObj.length != 0) newDataObj.push(monthObj);
});
本文标签: ecmascript 6Group array of objects by string property value in JavaScriptStack Overflow
版权声明:本文标题:ecmascript 6 - Group array of objects by string property value in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741503805a2382201.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论