0

Because JS is not like any other freaking language previously known to man! :/

Comments
  • 7
    Arrays are ref types in C# as well. Not that that is really a problem.
  • 10
    Yeah definitely the only language on the whole planet where its like that.
  • 1
    @SortOfTested it is a problem for me :'(
    JS is kinda of a new world to me, and i only need to program in it for the bootcamp project i am doing. I am mostly used to programming languages for scientific computing in the past. Matlab/FORTRAN earlier, and now Python etc.
  • 6
  • 4
    My question, why are you copying an array?
  • 0
    @C0D4 Its hard to explain!
    maybe this helps?

    //source: https://gist.github.com/tleen/...
    var states = ['Alabama', 'Alaska', 'American Samoa', etc etc...

    // Initialize totalCasesPerState with 0

    // Copy array and set it to zero
    var totalCasesPerState = [...states];
    totalCasesPerState.fill(0);

    for (let idx = 0; idx < array.length; idx++) {
    // console.log(`${array[idx].Province}: ${array[idx].Cases}`);

    var stateIndex = states.indexOf(array[idx].Province);

    if (stateIndex > -1) {
    totalCasesPerState[stateIndex] = totalCasesPerState[stateIndex] + array[idx].Cases;
    }
    }
  • 1
    @eisenhiem array is being passed as a function parameter. FYI!
  • 2
    @eisenhiem it feels like your trying to achieve this?

    https://gist.github.com/C0D4-101/...

    add your data set to a list of all regions/states?
  • 6
    Array.slice() and you get your copy.

    Side note : seriously? Are we gonna use the "copy vs reference" as an argument against js? Have we gone that low?
    Almost all languages have that. For good reasons!
    Check how computer memory works and all will be explained.
  • 1
    @C0D4 thanks for this. Really helpful.
  • 0
    @react-guy yeah well i didnt know this because in all other languages known to man (a man like me) you can just make a copy by doing array1 = array2 and ser array1 to zero and be sure that array2 wouldnt have been changed. Sorry if i offended anybody. Not my intention :(
  • 1
    Because pointers are totally only a JS thing. 🙄
  • 0
    @eisenhiem don't worry, not offended 😉 just joking.

    However, almost all the languages I know also use arrays as reference (Java, C/C++, idk about php...).

    Which languages do not?
Add Comment