0

Am I doing something wrong here? I have an array of tags, and when I do a jQuery each() on the array it doesn't go into the each() I did have an alert in the each but nothing happens. I have checked my error log console and there are no errors. So, what am I doing wrong?

var tags = new Array();
tags["video-games"] = "Video Games";
tags["sports"] = "Sports";
tags["movies"] = "Movies";
tags["board-games"] = "Board Games";
tags["news"] = "News";
tags["television"] = "Television";
tags["computers"] = "Computers";
tags["opinions"] = "Opinions";
tags["reviews"] = "Reviews";

function updateTags(){
    console.log(tags);
    $("div.tags > div > span:first-child").nextAll().remove();
    $.each(tags, function(key, val){
        $("div.tags > div").append("<span><a class='tag' href='/tags/" + key + "'>" + val + "</a></span>");

    });
}

updateTags();
3
  • 2
    Arrays don't work like that, use an object. Commented Oct 8, 2013 at 15:15
  • String keys are not supported javascript array Commented Oct 8, 2013 at 15:15
  • You used Array[] like Object {}. Commented Oct 8, 2013 at 15:19

3 Answers 3

10

Arrays are expected to have numeric indexes.
You've created an empty array which happens to have some properties.

You should create an ordinary object instead:

var tags = {
    "video-games": "Video Games",
    ...
};
Sign up to request clarification or add additional context in comments.

1 Comment

I agree. As you have a key-value pair use th eobject and use the Javascript for(var key in obj){console.log(obj[key]);} to get the expected results
0

So in this case

var tags = {};
tags["video-games"] = "Video Games";
tags["sports"] = "Sports";
tags["movies"] = "Movies";
tags["board-games"] = "Board Games";
tags["news"] = "News";
tags["television"] = "Television";
tags["computers"] = "Computers";
tags["opinions"] = "Opinions";
tags["reviews"] = "Reviews";

function updateTags(){
    //console.log(tags);
    $("div.tags > div > span:first-child").nextAll().remove();
    for(var key in tags){
        $("div.tags > div").append("<span><a class='tag' href='/tags/" + key + "'>" + tags[key] + "</a></span>");
    }
}

updateTags();

Should work.

1 Comment

don't forget about Object.hasOwnProperty (In this case I don't see it making a difference), when iterating object keys it is a good thing to remember
0

Your code is object and properties

var tags = {};
tags["video-games"] = "Video Games";

or

var tags = {
"video-games" : "Video Games";
};

Then

$.each(tags, function(key, val) {
    $("div.tags > div").append("<span><a class='tag' href='/tags/" + key + "'>" 
                                + val + "</a></span>");

});

3 Comments

in your second example you would get an invalid left hand side in assignment error because it sees the '-' as a operator and not part of the property name. Only bracket notation can be used for such names
@rlemon, Thanks for informing.. I didnt noted - :( I will get change it
your new example will also throw errors, you need to replace the '=' with ':' in object literal property assignments, and drop the semi-colon inside the object. var tags = { 'video-games' : 'Video Games' };

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.