根据对象的日期年份将对象数组拆分为新数组

时间:2022-02-27 12:17:51

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

我有一个名为objarray的对象数组。每个对象看起来像这样:

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)

(日期是Date对象)

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.

dateGroups中的每个数组都包含具有相同日期的对象。

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):

这可能与数组有关吗?以前我生成了一个新对象,其中包含按日期分组的所有objarray对象(从数据生成的日期):

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

以上看起来在实践中似乎是一个奇怪的解决方案,我只需要能够按年访问对象:即dateGroups [0] =第一年的对象数组

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

如何将数据转换为dateGroups数组?是否有更好的方法来存储此类数据?

3 个解决方案

#1


8  

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

考虑使用Underscore.js groupBy函数,然后使用sortBy。

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

如果你像这样调用它,groupBy会产生一个像alldates这样的结构:

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

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

sortBy可以用来简单地按键排序,如下所示:

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

You can also combine 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.

根据您的使用情况,我可以看到使用数组数组或数组对象映射的原因。如果您正在查找索引,请务必使用后者。

#2


0  

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];
}

#3


0  

An answer using reduce.

使用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.

第一个reduce用于按日期对对象进行分组,第二个用于使数组的键从0运行到不同年份的数量 - 而不是键是年份本身。

#1


8  

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

考虑使用Underscore.js groupBy函数,然后使用sortBy。

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

如果你像这样调用它,groupBy会产生一个像alldates这样的结构:

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

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

sortBy可以用来简单地按键排序,如下所示:

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

You can also combine 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.

根据您的使用情况,我可以看到使用数组数组或数组对象映射的原因。如果您正在查找索引,请务必使用后者。

#2


0  

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];
}

#3


0  

An answer using reduce.

使用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.

第一个reduce用于按日期对对象进行分组,第二个用于使数组的键从0运行到不同年份的数量 - 而不是键是年份本身。