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["puters"] = "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();
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["puters"] = "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();
Share
Improve this question
asked Oct 8, 2013 at 15:12
Get Off My LawnGet Off My Lawn
36.4k46 gold badges197 silver badges374 bronze badges
3
- 2 Arrays don't work like that, use an object. – rlemon Commented Oct 8, 2013 at 15:15
- String keys are not supported javascript array – u_mulder Commented Oct 8, 2013 at 15:15
-
You used
Array[]
likeObject {}
. – Murali Murugesan Commented Oct 8, 2013 at 15:19
3 Answers
Reset to default 10Arrays 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",
...
};
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["puters"] = "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.
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>");
});