1

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/

7
  • This code is in urgent need of either a switch or a dispatch table to kill off that endless pile of if statements. Commented May 18, 2017 at 16:21
  • Can you please show us the iteration over your data? Although your conditionals are ordered, that doesn't mean your data is. Commented May 18, 2017 at 16:21
  • 1
    Objects in an array will remain in the same position unless you alter their position. Commented May 18, 2017 at 16:21
  • 1
    block statements does not require a semicolon at the end. Commented May 18, 2017 at 16:24
  • Order of array elements will be determined based on when they are pushed and/or poped from the array. Unless you use sort or manually change position of the elements. Commented May 18, 2017 at 16:30

2 Answers 2

2

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];
});
Sign up to request clarification or add additional context in comments.

5 Comments

But like this I would always be pushing 'referral' right? So I should do a "pile" of these statements like in my example? Shouldn't I push i.channel?
Even with this the table_cat returns Referral,Direct,Social,(Other),Organic Search which is a mixed order
i don't really understand, what you woud like to achieve, an ordered data array or a smaller code of the above ifs. i edited the table_cat.push argument.
I'm trying to achieve an ordered array so I can reproduce something like this from my data. My problem is that I don't know why the objects in the table_cat don't follow the order of my if statements or your options dict
i suggest to sort the array after creating (in kind of random order). this gives the order, you need.
0

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.

Comments

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.