最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Split array of objects into new arrays based on year of object's date - Stack Overflow

programmeradmin8浏览0评论

I have an array of objects called objarray. Each object looks like this:

var object = {
    age: "45"
    coords: "-37.807997 144.705784"
    date: Sun Jul 28 2002 00:00:00 GMT+1000 (EST)
}

(date is a Date object)

I need to push each object into a new array based on the date. I want the end result to look like this:

var dateGroups = [[object, object, object],[object, object], [object, object, object]];

Each array within dateGroups contains objects with the same date.

Is this possible to do with arrays? Previously I generated a new object which contained all the objarray objects grouped by date (dates generated from the data):

var alldates = {
  "1991" : [object, object, object],
  "1992" : [object, object],
  //etc...
}

The above seems like a weird solution in practice though, I only need to be able to access the objects by year: i.e. dateGroups[0] = array of objects from the first year

How would I get the data into something like the dateGroups array? Is there a better way to store this type of data?

I have an array of objects called objarray. Each object looks like this:

var object = {
    age: "45"
    coords: "-37.807997 144.705784"
    date: Sun Jul 28 2002 00:00:00 GMT+1000 (EST)
}

(date is a Date object)

I need to push each object into a new array based on the date. I want the end result to look like this:

var dateGroups = [[object, object, object],[object, object], [object, object, object]];

Each array within dateGroups contains objects with the same date.

Is this possible to do with arrays? Previously I generated a new object which contained all the objarray objects grouped by date (dates generated from the data):

var alldates = {
  "1991" : [object, object, object],
  "1992" : [object, object],
  //etc...
}

The above seems like a weird solution in practice though, I only need to be able to access the objects by year: i.e. dateGroups[0] = array of objects from the first year

How would I get the data into something like the dateGroups array? Is there a better way to store this type of data?

Share Improve this question edited Jun 17, 2017 at 1:47 ekad 14.6k26 gold badges46 silver badges48 bronze badges asked Feb 14, 2012 at 4:28 tamarasaurustamarasaurus 4592 gold badges6 silver badges9 bronze badges 2
  • better to choose JSON for this. – Chamika Sandamal Commented Feb 14, 2012 at 4:35
  • What kind of JSON structure would be suitable to group by date? – tamarasaurus Commented Feb 14, 2012 at 4:38
Add a ment  | 

3 Answers 3

Reset to default 11

Consider using the Underscore.js groupBy function, followed by sortBy.

groupBy will produce a structure like alldates, if you call it like so:

var alldates = _.groupBy(objarray, function(obj) {
    return obj.date.getFullYear();
});

sortBy can be used to simply sort by key, like this:

var dateGroups = _.sortBy(alldates, function(v, k) { return k; });

You can also bine the two like this:

var dateGroups = _.chain(objarray)
                  .groupBy(function(obj) { return obj.date.getFullYear(); })
                  .sortBy(function(v, k) { return k; })
                  .value();

Depending on your use case, I can see reasons for using an array of arrays, or an object map of arrays. If you're looking up on index, definitely use the later.

An answer using reduce.

var ans = objects.reduce(function(prev,curr) {
    if (!prev[curr.date.getFullYear()]) prev[curr.date.getFullYear()] = [];
    prev[curr.date.getFullYear()] = curr;
    return prev;
},[]).reduce(function(prev,curr) {
    prev.push(curr);
    return prev;
},[]);

the first reduce is for grouping the objects by dates, and the second is for making the key of the array run from 0 to the number of different years - instead of the key being the year itself.

Once you have the dates in a structure that looks like this:

var alldates = {
  "1991" : [object, object, object],
  "1992" : [object, object],
  //etc...
}

You can just use this code to convert them to the structure you want:

var dateGroups = [];

for(var year in allDates){
   dateGroups[dateGroups.length] = allDates[year];
}
发布评论

评论列表(0)

  1. 暂无评论