All the error means in JSHint/JSLint is that you didn't declare your key/iterator variable. As @Christopher suggests, JSLint wants you to declare it at the top of its scope (google JavaScript hoisting
for more on hoisting, like this link):
/*global data, identifier, DIVsuggestions */
// We'll pretend all of the above were passed in from a function's parameters
// by using JSLint's "global" keyword -- now you can paste this code into
// jslint.com and have it pass muster.
// make array
var sugested_sports = data.split(","),
sporty_items = '', // pre build DIV
sport; // <<<< **** DECLARE YOUR "KEY" HERE ****
for (sport in sugested_sports)
{
if (sugested_sports.hasOwnProperty(sport)) {
sporty_items += '<a href="#'+identifier[1]+'">'
+sugested_sports[sport]+'</a>';
}
}
// insert DIV
DIVsuggestions.html(sporty_items);
This bad for in variable
error here reduces to the same as a 'sport' was used before it was defined
error elsewhere.
EDIT: It's worth mentioning that if your for
is in an internal function, you need to declare your for in
variable in that same context. JSLint will complain if you declare the for in
in the parent context.
Example:
function spam(d)
{
var fnTest, row; // `row` is defined "too early"
fnTest = function (data) {
for (row in data)
{
if (data.hasOwnProperty(row))
{
console.log(data.row);
}
}
};
fnTest(d);
}
To make things happy, move row
into the internal function. Even though it was technically still in scope, JSLint doesn't like the "superscope" that was used before.
function spam(d)
{
var fnTest;
fnTest = function (data) {
var row; // and JSLint is happy! ;^D
for (row in data)
{
if (data.hasOwnProperty(row))
{
console.log(data.row);
}
}
};
fnTest(d);
}
By the way, James' concern is covered by the
hasOwnProperty
check the OP has inserted. Take out that check, and JSLint will complain, "The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype".
Here's a little more on hasOwnProperty with for... in, if you're interested.