0

I use a forEach() loop, inside which I modify a (associative) array declared earlier.

But whatever I try, it seems the change only happen inside the if/else, and is ignored outside it.

What did I do wrong?

var referer = [],
    r,
    result = [
        { _id: 'http://www.url1.com/video/agenda-du-mois-de-juin-6/', count: 3 },
        { _id: 'http://www.url2.com/video/reaction-de-bertrand-rocheron/', count: 3 },
        { _id: 'http://www.url1.com/video/victoire-du-rac-basket-3/', count: 30 },
        { _id: 'http://www.url3.com/embed-3194.html', count: 3 },
        { _id: 'http://www.url2.com/categorie-video/actu/page/10/', count: 9 },
        { _id: 'http://www.url1.com/video/agenda-de-septembre/', count: 4 },
        { _id: 'http://www.url2.com/video/le-journal-du-mardi-18-mars/', count: 8 },
        { _id: 'http://www.url3.com/video/programme-du-6-avril-2012/', count: 1 } 
    ];

console.log(result);

result.forEach(function (element, i) {
    //Get domain
    r = element._id.replace('http://', '').replace('www.', '').split('/')[0];
    console.log(r);
    //If it exist
    if (referer[r]) {
        //Add data in it
        console.log('\tADD    : ' + r);
        referer[r].y = referer[r].y + element.count;
    } else {
        //Else create it
        console.log('\tCREATE : ' + r);
        referer[r] = {name: r, y: element.count};
    }
    //Why do this is still empty...?
    console.log('\tTOTAL  : ' + JSON.stringify(referer));
});
//...When I need to use it here?
console.log('result: ' + JSON.stringify(referer));

Output:

url1.com
        CREATE : url1.com
        TOTAL  : []
url2.com
        CREATE : url2.com
        TOTAL  : []
url1.com
        ADD    : url1.com
        TOTAL  : []
url3.com
        CREATE : url3.com
        TOTAL  : []
url2.com
        ADD    : url2.com
        TOTAL  : []
url1.com
        ADD    : url1.com
        TOTAL  : []
url2.com
        ADD    : url2.com
        TOTAL  : []
url3.com
        ADD    : url3.com
        TOTAL  : []
result: []
DrakaSAN
  • 7,673
  • 7
  • 52
  • 94
  • Two suggestions: 1) Make `referer` an object, not an array, because you're not using it like an array. 2) Use `require('util').inspect()` instead of `JSON.stringify()` because `JSON.stringify()` only supports a limited number of data types. – mscdex Sep 03 '14 at 15:35
  • It's empty since you are never adding a new element to it. For some reason, `referer[r]` is always truthy. Is this really the code you are using? Also note that `JSON.stringify` will ignore all non-numeric properties of an array, as explained here: http://stackoverflow.com/q/4425289/218196 – Felix Kling Sep 03 '14 at 15:38
  • @mscdex: I would swear I already tried that, but for some reason, it just worked again when I maked referer an object... – DrakaSAN Sep 03 '14 at 15:42
  • @FelixKling: It was a error when I c/p the output, it s not always truthy. Yes it is, checked again while editing. – DrakaSAN Sep 03 '14 at 15:43
  • Well, then it's a duplicate of the question I linked to. – Felix Kling Sep 03 '14 at 15:44
  • @FelixKling: It is, I didn t thought it of searching this like that thought. – DrakaSAN Sep 03 '14 at 15:45
  • @FelixKling: Should I flag myself as duplicate? Or silently delete? – DrakaSAN Sep 03 '14 at 15:46
  • I already closed it as duplicate. – Felix Kling Sep 03 '14 at 15:46

0 Answers0