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

时间:2012-10-22 14:57:23

标签: javascript

我有一个数组,其值附加了日期。

-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"

如何根据此数组中的类似日期创建多个数组。例如。我想要的一天结束。

data1数组将包含:

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

data2数组将包含:

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

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"

我们将不胜感激。

4 个答案:

答案 0 :(得分:1)

如果我正确理解你,我可能会循环维护一个由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;
});

现在data[0]包含一系列条目,time值最低,data[1]下一个最高time值等等。

答案 1 :(得分:1)

这种方法怎么样:

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

因此对于测试数据:

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

它将创建下一个结构的对象(而不是数组):

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"}
    ]
}​

答案 2 :(得分:1)

这将为您提供一个阵列数组。

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;
}, []);

我假设您实际上并不想为每个数组添加单独的递增标识符,因为这通常不是很有用。

http://jsfiddle.net/w6qE9/

结果:

[
    [
        {
            "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"
        }
    ]
]

答案 3 :(得分:0)

从你的问题中确切地知道你想要实现什么样的结果,但是这里有一个函数可以为你提供一个数组,每个时间值都有一个数组:

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