I have a return from a javascript function that looks like this:
return{"success": true, "message": "Password changed."};
How do i retrieve these when calling this funciton?
I have a return from a javascript function that looks like this:
return{"success": true, "message": "Password changed."};
How do i retrieve these when calling this funciton?
You are returning an Object, You can save it in a variable and then can access the object properties.
//function definition
function fun1(){
return{"success": true, "message": "Password changed."};
}
//function calling
var res1 = fun1();
//using the result returned by function call
if(res1.success)//true
{
alert(res1.message);//"Password changed.
}
function functionReturningObject(){
return{"success": true, "message": "Password changed."};
}
// use a temporary variable!!! this is a valid syntax but will execute twice
// success = functionReturningObject().success
// message = functionReturningObject().message
var map = functionReturningObject();
// when you know it contents you refer it directly
console.log(map.success)
console.log(map.message)
// when you do not know it contents, you can "explore" it
Object.keys(map).forEach(function(key){console.log(key);console.log(map[key]);})