I have a result from my ajax call:
function getNames() {
var names=[];
(myAjaxCallHere).success(result) {
for(i in result) {
names.firstname = result[i].firstname;
}
return names;
}
}
when I call the function getNames
var names = getNames();
console.log(names);
the result is:
>[]
when i expand, the result is :
v Array[0]
firstname:"JohnDoe"
length:0
> __proto__:Array[0]
I tried to extract:
var names = getNames();
console.log(names.firstname);
but the result is undefined
I tried also
for(i in names) {
console.log(names[i].firstname);
}
but still no luck.
Uncaught TypeError: Cannot read property 'firstname' of undefined
How do I print firstnames in console using javascript? This is my first step to learn javascript. Please help. thanks :)