Array.each () loop returns a string instead of a value in jQuery

Sorry, this might be easy, but it puzzled me. I am trying to iterate over this array and exit each value, but the script produces a string object.

propertiesToSanitize = ["title", "description", "place_name"]
$.each propertiesToSanitize, ->
  console.log this

which translates to jQuery as

var propertiesToSanitize;
propertiesToSanitize = ["title", "description", "place_name"];
$.each(propertiesToSanitize, function() {
  return console.log(this);
});

returns:

String
  0: "t"
  1: "i"
  2: "t"
  3: "l"
  4: "e"
  length: 5

Any idea why she returns this instead of "title" or any other value? Thanks in advance for any help.

+3
source share
2 answers

the index and value for each iteration are provided as a parameter in the callback.

$.each(propertiesToSanitize, function(index,value) {
    console.log(index + ':' + value);
});
+8
source

, this JavaScript. this - , . jQuery each, "Javascript this Object, ", , .

jQuery each . JavaScript , , :

$.each(propertiesToSanitize, function(idx, val) {
  return console.log(idx + ":" + val);
});

, CoffeeScript jQuery:

for propName in propertiesToSanitize
  console.log propName
+2

All Articles