0

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. E.g. 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.

3
  • 1
    Whats with the - symbol, is x always sorted correctly? Commented Oct 22, 2012 at 15:02
  • 1
    This doesn't make any sense. You speak of data1 array and data2 array, but they are all in the same x array? Commented Oct 22, 2012 at 15:03
  • Alex, no the data cant be sorted correctly always. However in this case the dates are already sorted so thats why they remain the same. But all is fixed thanks. Commented Oct 24, 2012 at 12:36

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry for late response. But thanks guys this worked out quite well. Exactly what i wanted.
@user1469174: Good deal, glad that helped!
1

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

1 Comment

Worked Great. At first i was getting no values, but is fixed now. thanks
1

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

1 Comment

Thanks for your immediate help. It helped a lot because i wanted to create something like this for easy calling to a chart.
0

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

2 Comments

I dont really care about dates. I wanted the values just sorted into dates so i can use them. Thanks, this was quite helpful.
Just thought i should say, reworked my code and this works like a charm thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.