简体   繁体   中英

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:

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

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

data3 array will contain:

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

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.

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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