简体   繁体   English

从现有数组创建新数组,该数组包含带有日期的值

[英]Creating new arrays from an existing array that holds values with dates

I have an array with values that have dates attached to them. 我有一个值附加了日期的数组。

-x[0].value = 5,    x[0].time = "Mon 24 April 2012"
-x[1].value = 12,   x[1].time = "Mon 24 April 2012"
-x[2].value = 11,   x[2].time = "Mon 23 April 2012"
-x[3].value = 2,    x[3].time = "Mon 20 April 2012"
-x[4].value = 11,   x[4].time = "Mon 20 April 2012"
-x[5].value = 7,    x[5].time = "Mon 20 April 2012"
-x[6].value = 7,    x[6].time = "Mon 20 April 2012"

How do i create many arrays according to similar dates from this array. 如何根据该数组的相似日期创建许多数组。 Eg end of the day i want. 例如,我想结束一天。

data1 array will contain: data1数组将包含:

-x[0].value = 5,    x[0].time = "Mon 24 April 2012"
-x[1].value = 12,   x[1].time = "Mon 24 April 2012"

data2 array will contain: data2数组将包含:

-x[2].value = 11,   x[2].time = "Mon 23 April 2012"

data3 array will contain: data3数组将包含:

-x[3].value = 2,    x[3].time = "Mon 20 April 2012"
-x[4].value = 11,   x[4].time = "Mon 20 April 2012"
-x[5].value = 7,    x[5].time = "Mon 20 April 2012"
-x[6].value = 7,    x[6].time = "Mon 20 April 2012"

Your help will be appreciated. 您的帮助将不胜感激。

If I'm understanding you correctly, I'd probably loop through maintaining a temporary map keyed by the time values, and sort at the end: 如果我对您的理解正确,则可能会循环维护一个由time值作为键的临时映射,并在末尾进行排序:

var index;
var data;
var rentry;
var entry;
var map;

data = [];
map = {};
for (index = 0; index < x.length; ++index) { // Or forEach on ES5 systems
    entry = x[index];
    rentry = map[entry.time];
    if (!rentry) {
        rentry = map[entry.time] = [];
        data.push(rentry);
        rentry.time = entry.time;
    }
    rentry.push(entry);
}
map = undefined;
data.sort(function(a, b) {
    if (a.time < b.time) {
        return -1;
    }
    if (a.time > b.time) {
       return 1;
    }
    return 0;
});

Now data[0] has an array of entries with the lowest time value, data[1] the next highest time value, etc. 现在, data[0]具有一个具有最低time值的条目数组, data[1]具有第二高time值,等等。

What about this approach: 那么这种方法呢:

var arrs = {};
for (var i = 0; i < data.length; i++) {
    if (!arrs[data[i].time]) arrs[data[i].time] = [];
    arrs[data[i].time].push(data[i])
}

So for test data: 因此对于测试数据:

var data = [
    {value: 5, time: 'Mon 24 April 2012'},
    {value: 12, time: 'Mon 24 April 2012'},
    {value: 11, time: 'Mon 23 April 2012'},
    {value: 2, time: 'Mon 20 April 2012'},
    {value: 11, time: 'Mon 20 April 2012'},
    {value: 7, time: 'Mon 20 April 2012'},
    {value: 7, time: 'Mon 20 April 2012'},
];

It will create an object (not array) of the next structure: 它将创建下一个结构的对象(而不是数组):

arrs = {
    "Mon 24 April 2012": [
        {"value": 5, "time": "Mon 24 April 2012"},
        {"value": 12, "time": "Mon 24 April 2012"}
    ],
    "Mon 23 April 2012": [
        {"value": 11, "time": "Mon 23 April 2012"}
    ],
    "Mon 20 April 2012": [
        {"value": 2, "time": "Mon 20 April 2012"},
        {"value": 11, "time": "Mon 20 April 2012"},
        {"value": 7, "time": "Mon 20 April 2012"},
        {"value": 7, "time": "Mon 20 April 2012"}
    ]
}​

This will give you an Array of Arrays. 这将为您提供一个数组数组。

var dataGroups = data.sort(function(a, b) {
    return a.time.localeCompare(b.time);
}).reduce(function(result, obj) {
    if (result.length && obj.time === result[0][0].time)
        result[0].push(obj);
    else
        result.unshift([obj]);
    return result;
}, []);

I assume you didn't actually want separate incrementing identifiers for each Array, since that's usually not very useful. 我假设您实际上并不需要每个数组的单独的增量标识符,因为这通常不是很有用。

http://jsfiddle.net/w6qE9/ http://jsfiddle.net/w6qE9/

The result: 结果:

[
    [
        {
            "value": 5,
            "time": "Mon 24 April 2012"
        },
        {
            "value": 12,
            "time": "Mon 24 April 2012"
        }
    ],
    [
        {
            "value": 11,
            "time": "Mon 23 April 2012"
        }
    ],
    [
        {
            "value": 2,
            "time": "Mon 20 April 2012"
        },
        {
            "value": 11,
            "time": "Mon 20 April 2012"
        },
        {
            "value": 7,
            "time": "Mon 20 April 2012"
        },
        {
            "value": 7,
            "time": "Mon 20 April 2012"
        }
    ]
]

It is unclear from your question exactly what kind of result you are trying to achieve, but here is a function that will give you an array of arrays, one array for each time value: 从您的问题尚不清楚,您到底想获得什么样的结果,但是下面的函数将为您提供一个数组数组,每个时间值一个数组:

var map = {}, i, time, temp, results;
for (i = 0; i < x.length; i++) {
    time = x[i].time;
    if (time in map) {
        // add this item to the array we already have for this time
        map[time].push(x[i]);
    } else {
        // create a new array for this time and put it in the map
        temp = [];
        temp.push(x[i]);
        map[time] = temp;
        results.push(temp);
    }
}

// here the variable results is an array of arrays, one for each time value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM