0

I currently use this code to iterate through a .json file:

for (f = 0; f < forum.length; f++) {
            end = columnArray.length - 1;
            object = forum[f];
            for (property in object)
                    {
                    value = object[property];
                    if (property === columnArray[end]) {
                        tableRowData = "<td>" + value + "</td></tr>";
                    } else {
                        tableRowData = "<td>" + value + "</td>";
                    }
                    tableRowData2 += tableRowData;
                    tableRowData = "";
                }
            finalTableData = "<tr>" + tableRowData2;
            finalTableData2 += finalTableData;
            tableRowData2 = "";
        }

JSLint doesn't like the idea of using for/in loops to iterate through arrays. Was trying to write it as a standard:

for (i = 0; i < forum.length; i++) {}

But got stuck. This is probably simple, but I cannot see what the logic should be for some reason.

Thanks.

Ivan Chaer
  • 6,980
  • 1
  • 38
  • 48
Jeff Mcbride
  • 453
  • 2
  • 6
  • 19
  • Not sure what you are asking – vinayakj Nov 05 '15 at 20:01
  • I want to iterate through the json file without using a for/in loop. – Jeff Mcbride Nov 05 '15 at 20:03
  • If about `for (property in object)` then use `for (var i = 0; i < object.length; i++) {object[i]} ` – vinayakj Nov 05 '15 at 20:03
  • 2
    Can you give an example of your JSON file? – Kevin Ji Nov 05 '15 at 20:04
  • I wanted to make sure that I iterated through the file without knowing what the key is but still being able to get the value – Jeff Mcbride Nov 05 '15 at 20:08
  • Please define "JSLint doesn't like" – mentat Nov 05 '15 at 20:09
  • `forEach` works nicely – Sterling Archer Nov 05 '15 at 20:09
  • you code seems to complicated, i think here enought two `map` functions – Grundy Nov 05 '15 at 20:12
  • I would use `for (var f...)` to state the variable in a local scope, then if you really need to use the for loop instead Array.forEach, check out jsLint provides a "tolerate for statement" checkbox option, by default unchecked. – bruno.bologna Nov 05 '15 at 20:18
  • According to this: http://stackoverflow.com/a/9329476/1575395. I can use .hasOwnProperty() to keep the vairiable in the function and "...as of ES6, the order is guaranteed...". JSLint may throw it as an error that I shouldn't use a for/in, but all current browsers and some older ones should handle it just fine so I'm not going to worry about it unless it becomes an issue with any browser. – Jeff Mcbride Nov 05 '15 at 22:17

1 Answers1

0

I see two things JSLint might be complaining about, so I'll answer them both.

For the outer loop, JSLint doesn't like the ++ family of constructs (including the -- variants), because they can be sensitive to whitespace or the lack thereof. Your outer loop can be rewritten as:

for (i = 0; i < forum.length; i += 1) {}

...and JSLint shouldn't complain about that.

For the inner loop, JSLint wants you to put a hasOwnProperty guard into your for-in loop. That might work like this...

for (property in object) {
    if (object.hasOwnProperty(property)) {
        value = object[property];
        if (property === columnArray[end]) {
            tableRowData = "<td>" + value + "</td></tr>";
        } else {
            tableRowData = "<td>" + value + "</td>";
        }
        tableRowData2 += tableRowData;
        tableRowData = "";
    }
}

Note that I've wrapped the body of the loop in an if-check on Object.prototype.hasOwnProperty() (which, since arrays are objects, they can use as method calls). This protects against unintended consequences from properties back in the prototype chain that might otherwise get enumerated.

The Spooniest
  • 2,863
  • 14
  • 14
  • JSLint still doesn't like the .hasOwnProperty. Still says "Bad for in variable 'property'". Also, forgot about the ++ and --. I'm changing the ++'s to +=. – Jeff Mcbride Nov 05 '15 at 20:33
  • That's a different issue. Where are you defining `property` as a variable? – The Spooniest Nov 06 '15 at 16:17
  • All variables are defined at the top of the document. What I'm seeing is that JSLint doesn't want you to use for/in loops as they can cause problems. Adding .hasOwnProperty (per ES6 apparently) makes it acceptable to use a for/in loop. From another post: "You'll get people telling you to use for-in, but that's not what for-in is for. for-in loops through the enumerable properties of an object, not the indexes of an array. Up through ES5, the order is not guaranteed; as of ES6, the order is guaranteed (by [[OwnPropertyKeys]], [[Enumerate]], and the definition of for-in/for-of)." – Jeff Mcbride Nov 06 '15 at 20:06