-2

I'm going through some code at work that someone else wrote, and I'm still relatively new to JS, and I have a question about a pattern I'm seeing.

I'm finding the below pattern of using .join() and then .split(',') on an array to make a new array in a few places in this old code, usually dealing with parsing data returned from an HTTP request, but not always. I can't understand the reasons for this. Here's the snippet I'm wondering about, and then a larger snippet to give context.

var first = ['listColumn1','listColumn2','listColumn3'].join();
var headers = first.split(',');
function getEntityLookup(){
    $.ajax({
        type:"GET",
        url:uri,
        dataType: "json",
        async: true,
        success: parseData,
        failure: function(data) {console.log("Error - could not be found");}
    })
}
function parseData(result){
    console.log(result);
    var first = ['listColumn1','listColumn2','listColumn3'].join();
    var headers = first.split(',');
    for ( var i = 0, length = result.length; i < length; i++ ){
        var myRow = result[i].join();
        var row = myRow.split(',');
        var data = {};
        for ( var x = 0; x < row.length; x++ ){
            data[headers[x]] = row[x];
        }
        jsonData.push(data);
    }

Why would anyone do this? first and myRow are not referenced again after this operation.

I can see the purpose for this if you're trying to copy an array and want to have two separate references, so you're not just getting a copied reference (I know just enough to know that there's the potential for two variables referencing the same actual array in memory if you do this wrong, but I imagine there's probably a better way to make a separate memory allocation than .join() then .split(',') ); however, the first array is never referenced again in any of the code I've looked at with this pattern, so that doesn't seem to be the purpose in this case.

asmedley
  • 1
  • 2
  • 1
    Not obvious to me. Time for `git blame` and then go ask the original author? Or perhaps there was historically some additional code that could explain this. – jarmod Apr 03 '23 at 21:48
  • Most charitable explanation I can think of is that this code has been adapted from code that dealt with raw CSV format, but it was not adapted thoughtfully. It's certainly doing no good now. – motto Apr 03 '23 at 22:02
  • It does make a difference if any of the column names contains a comma, but for the example code in your question where they are hardcoded it makes absolutely zilch sense. – Bergi Apr 03 '23 at 23:41
  • Thank you for the comments. I didn't know if I was just totally missing something fundamental about JavaScript. – asmedley Apr 04 '23 at 16:03
  • They are cloning the array. Does not make much sense, but that is what they are doing. – epascarello Apr 04 '23 at 21:29
  • @epascarello Is there a better way to clone an array? That's a tangential part of my question, I suppose. – asmedley Apr 06 '23 at 16:45
  • There is no reason to clone the array in that situation.... If first was declared out side of parseData then you might want to clone it to prevent actions taken on the array to change the values on the indexes. There are plenty of ways to clone/copy a array https://stackoverflow.com/questions/3978492/fastest-way-to-duplicate-an-array-in-javascript-slice-vs-for-loop – epascarello Apr 06 '23 at 17:03

1 Answers1

0

Well if first and myRow are not referenced again after this operation. it maybe means they were used for testing, or are global variables or the dev forget to delete them. In any case, if you are asking about the order of operations .join() was used first because you can't .slice() on an array see here. So he first made it a string using .join() that returns a new string.

VladTbk321
  • 73
  • 8
  • Did you mean to say you can't `.split()` on an array? Yes, I understand the order here. The question is mainly why the two references, and I think you are probably right there. – asmedley Apr 06 '23 at 16:43