Here is some output that I get from firebug
<div id='div_undefined'>[{"Region":"AD","Date":"","ArticleID":"abc-345"}, {"Region":"CD","Date":"","ArticleID":"abc-345"}]</div>
You can see the JS Object array in there and notice the div_undefined. If you look at the funciton below I know that my object is defined because it is printing out. Why would Items[0] return undefined when I try to access ArticleID
here is the code from my function
function myFunc(Items){
var item = Items[0];
Write("<div id='div_" + item.ArticleID + "'>" + Items + "</div>");
}
If concatenating
Itemsproduces this output, then it’s a string (containing JSON) and you have to parse it to a JS data structure first (an array in this case):Ideally you would do this before you pass the value to the function. I don’t know which output you are after, so that’s all I can advice.
Otherwise,
Items[0]refers to the first letter of the string (a string as well) and strings don’t have anArticleIDproperty, hence you getundefined.You might find this question helpful as well: Access / process (nested) objects, arrays or JSON And as mentioned by Matt (and in the other question), if you want to debug your code, use
console.log.