Costantin
Costantin

Reputation: 2656

Javascript order of an array. Is it random?

I need to be able to forecast the order of the objects in an array. My array will be formed by arrays which contain 3 values, an X axis, a Y axis and a value. The Y axis correspond to to the Objects I add in the array, so when I add for example Other I need to make sure that is the first element in the array, otherwise my data get's mixed up. I don't know if a particular element exists.

So far I have a loop with this:

            if (i.channel == '(Other)') {
              var pv_on_session = i.pageviews/i.sessions
              table_data.push([0, 0, pv_on_session.toFixed(2) ]) // pv/sess, other, pv
              table_data.push([1, 0, parseInt(i.avg_time)])
              table_cat.push('(Other)')
            };

            if (i.channel == 'Social') {
              var pv_on_session = i.pageviews/i.sessions
              table_data.push([0, 1, pv_on_session.toFixed(2) ]) // pv/sess, social, pv
              table_data.push([1, 1, parseInt(i.avg_time)])
              table_cat.push('Social')
            };

            if (i.channel == 'Direct') {
              var pv_on_session = i.pageviews/i.sessions
              table_data.push([0, 2, pv_on_session.toFixed(2) ]) // pv/sess, other, pv
              table_data.push([1, 2, parseInt(i.avg_time)])
              table_cat.push('Direct')

           if (i.channel == 'Organic Search') {
              var pv_on_session = i.pageviews/i.sessions
              table_data.push([0, 3, pv_on_session.toFixed(2) ]) // pv/sess, other, pv
              table_data.push([1, 3, parseInt(i.avg_time)])
              table_cat.push('Organic Search')
            };


            if (i.channel == 'Referral') {
              console.log('xx is: '+ i.avg_time)
              var pv_on_session = i.pageviews/i.sessions
              table_data.push([0, 4, pv_on_session.toFixed(2) ]) // pv/sess, other, pv
              table_data.push([1, 4, parseInt(i.avg_time)])
              table_cat.push('Referral')
            };

etc

From my understanding if the channel (other) exists it will add into the array the [0 (x axis), 0(y axis, so first element in the array so (other), value] If it doesn't it will skip this and go to the next one.

However when I print table_cat I get my array in a random(?) order, like Referral,Direct,Social,(Other),Organic Search Why this? What I'm missing?

EDITS:

So my data looks like this, however I don't know which channels could be there, but I do know the options:

data =
[{
"channel": "(Other)",
"pageviews": 1388082,
"sessions": 314263,
"avg_time": 54.94890183937861
}, {
"channel": "Referral",
"pageviews": 364869,
"sessions": 50387,
"avg_time": 58.104753437736335
}, {
"channel": "Direct",
"pageviews": 92538,
"sessions": 22118,
"avg_time": 59.21658970091479
}, {
"channel": "Organic Search",
"pageviews": 23470,
"sessions": 4246,
"avg_time": 51.96215449005384
}, {
"channel": "Social",
"pageviews": 8317,
"sessions": 1931,
"avg_time": 58.63430399702078
}]

Options are:

        // (Other) = 0
        // Social = 1
        // Direct = 2
        // Organic Search = 3
        // Referral = 4
        // Paid Search = 5
        // Email = 6
        // Affiliates = 7
        // Other Advertising = 8
        // Display = 9
        // Email = 10

I loop through them with a simple loop like:

            data.forEach(function(i){

            if (i.channel == '(Other)') { 
       ....etc

I want to achieve this table http://jsfiddle.net/cosbgn/vwmw75hx/1/

Upvotes: 1

Views: 88

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386680

You could use an object as reference for the needed number and use only one if statement.

var options = {
        '(Other)': 0,
        'Social': 1,
        'Direct': 2,
        'Organic Search': 3,
        'Referral': 4
    };

if (i.channel in options) {
    var pv_on_session = i.pageviews/i.sessions;
    table_data.push([0, options[i.channel], pv_on_session.toFixed(2)]); // pv/sess, other, pv
    table_data.push([1, options[i.channel], parseInt(i.avg_time)]);
    table_cat.push(i.channel);
}

For sorting your data array, you could use the same object as above for getting the sort order.

data.sort(function (a, b) {
    return option[a.channel] - option[b.channel];
});

Upvotes: 2

Costantin
Costantin

Reputation: 2656

So for who arrives here through Google. I'm still not entirely sure why my array is not ordered as it should, but I manage to solve this with a simple count starting from -1.

Something like this:

var options = [];
var table_data = [];
var count = -1;

channels.forEach(function(i) {
  options.push(i.channel)
  if (options.includes(i.channel)) {
    count += 1
    var pv_on_session = i.pageviews / i.sessions
    table_data.push([0, count, pv_on_session.toFixed(2)]) // pv/sess, other, pv
    table_data.push([1, count, parseInt(i.avg_time)])
  }
})

This said I marked correct the answer of @nina-scholz because it removes all the if loops and let to this.

Upvotes: 0

Related Questions