I'm sure I'm doing it all wrong, but I have the following function returning "undefined" in the console, although it can console.log () the requested values ββfrom the same place in the function as the code commented.
var tags = [4, 5]; console.log(getTagNames(tags)); // 'undefined' function getTagNames(tagArray) { $.getJSON('js/tags.json', function(data) { for (var i in tagArray) { tagArray[i] = tagArray[i].toString(); var val = tagArray[i]; for (var t in data) { var tag = data[t]; var tagName = tag.alias; var tagId = tag.id; if (val === tagId) { tagArray[i] = tagName; } }; } console.log(tagArray); // output ["foo", "bar"] return tagArray; }); }
Another thing is that after running this code in the browser, I can enter the "tags" in the browser console, and it gives the correct result ["foo", "bar"] . However, when I try to use a tag variable (for example, a text value for an element, etc.), it does not work ... What gives? JavaScript is not my first language, so I'm a little confused about how it behaves. I just do not understand.
I read almost all the "Questions that may already have my answer," but the answers were provided where I could not figure out how to apply to my function.
Note:
- JSON from the Joomla tag table (3.1).
- I can get the data.
- The value val === tagId conditionally works correctly.
- I like popcorn.
source share