3

I have loaded jquery and I have an array (object) containing strings like so:

window.stack = [
  "header",
  "nav",
  ".content",
  "footer"
];

When I run this through a loop using jquery's each() function and try to get each of my strings back again like this:

$.each(window.stack,function(){
    var h = this;
    console.log(h);
})

...I get this:

String [ "h", "e", "a", "d", "e", "r" ]
String [ "n", "a", "v" ]
String [ ".", "c", "o", "n", "t", "e", "n", "t" ]
String [ "f", "o", "o", "t", "e", "r" ]

Why don't I just get:

header
nav
.content
footer

?

1

2 Answers 2

2

You can use $.each(window.stack, function(key, value).

From http://api.jquery.com/jquery.each/

The value can also be accessed through the this keyword, but Javascript will always wrap the this value as an Object even if it is a simple string or number value.

So, if you want to use this,

$.each(window.stack,function(){
 var h = this;
 console.log(h.toString());
})
Sign up to request clarification or add additional context in comments.

1 Comment

"Javascript will always wrap the this value as an Object even if it is a simple string or number value" <-- That's the answer right there, I didn't get this. Thanks.
1

you can try this

  $.each(window.stack,function(key,value){

    console.log(value);
})

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.