Trip
Trip

Reputation: 27114

What is wrong with my syntax iterating through an array of strings?

array_of_strings = ["this", "is", "my", "array", "of", "strings"]

array_of_strings.each(function(val, i) {  console.log(i)  })

Which returns :

TypeError: Object has no method 'each'

I thought I could iterate through an array this way..

Upvotes: 2

Views: 48

Answers (1)

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

What you have is for iterating jQuery objects. You should use $.each like below,

//                       index----v   v-----value
$.each(array_of_strings, function(i, val) {  console.log(val)  });

Also the params inside $.each function is (index,value)

Upvotes: 5

Related Questions