I'm reading big csv files and mashing the lines into hierarchical objects for analysis and display. The csv might look like this:


My non-clever reading code works but I dislike it; it goes like this (could be typos from editing down):

function makePlotData() {
if(ground.reader.readyState==4) {
    var lines = ground.reader.responseText.split(/\r\n?/);
    for (var i=0; i<lines.length; i++) {
        if (lines[i].match(/^\d\d\d\d,/)) {     // then it's a regular csv line
            var fieldsArr = lines[i].split(',');
            var [year, quad, spec] = [fieldsArr[0],fieldsArr[1],fieldsArr[2]];
            ground[quad] = ground[quad] || {};
            ground[quad][year] = ground[quad][year] || {};
            ground[quad][year][spec] = ground[quad][year][spec] || {};
            ground[quad][year][spec].sum = ground[quad][year][spec].sum || 0;
            ground[quad][year][spec].sizes = ground[quad][year][spec].sizes || [];
            var sizeArr = fieldsArr.slice(5).map(Number);
            if (ground[quad][year][spec].sizes.length) {  // already have a size classes Arr
                for(var s=0;s<sizeArr.length;s++){  // ...so add to it
                    ground[quad][year][spec].sizes[s] += sizeArr[s];}
            } else {ground[quad][year][spec].sizes = sizeArr;}
        } // ... continue with assignments and doing stuff

My objection is to that series of statements like ground[quad] = ground[quad] || {}; , which do what I need but seem like a lot of unnecessary assigning and reassigning; I bet it doesn't matter but it gives me a feeling of wasted cycles and room for errors; it's also ungainly and a nuisance to modify. Is there any idiom or anything that manages this more efficiently or is this as good as it gets with this kind of structure?

  • \$\begingroup\$ @200_success Hi 200 -- is that title edit according to a site stylebook or something? It is indeed a neater phrase, but I find the googling results are a lot more on-point when the titles include the language in question. \$\endgroup\$
    – uhClem
    Jul 14, 2016 at 16:41
  • \$\begingroup\$ Yes, that is the site standard— also see How to Ask. \$\endgroup\$ Jul 14, 2016 at 16:45


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.