0

我有一个带有附加日期的值的数组。

-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

4 回答 4

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]的条目数组。timedata[1]time

于 2012-10-22T15:07:38.153 回答
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"}
    ]
}​
于 2012-10-22T15:08:41.737 回答
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"
        }
    ]
]
于 2012-10-22T15:11:52.897 回答
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
于 2012-10-22T15:08:03.140 回答