console.log in the function "getNameByDate" will produce "Ben" and "Tom". But the other console.log outside of the function will produce "undefined". Why not the same output?
let arr = [
{
databaseObject: { geburt: "24.01.2012", name: "Ben"}
},
{
databaseObject: { geburt: "29.02.2012", name: "Tom"}
},
];
function getNameByDate(date) {
jQuery.each( arr, function( i, val ) {
if ( val.databaseObject.geburt == date ) {
console.log (val.databaseObject.name);
return val.databaseObject.name;
}
});
}
let dates = ["24.01.2012", "29.02.2012"];
jQuery.each( dates, function( i, val ) {
console.log(getNameByDate(val));
});
function (i, val) ...is also a function and the return inside it belongs tofunction (i, val) ...notgetNameByDate(). The functiongetNameByDate()returns nothing which in javascript is the same asundefined