I have a function that's supposed to loop through a JSON file, get the values and push them to a new array.
I have declared the array outside the function as follows :
var output = [];
function get_json(jsonObj) {
console.log("Out put => " + output);
for (var x in jsonObj) {
if (typeof (jsonObj[x]) == 'object') {
get_json(jsonObj[x]);
} else {
output.push({
key: x,
value: jsonObj[x]
});
//console.log(output);
}
}
return output;
}
The above function is called and passed into the json data as follows :
var result = get_json(jsonObj);
Which is supposed to return an array with values, a key, and a value. However, when I push data to the function, I get the output variable to be undefined so it cannot create an array, leading to a failure. How can I declare the array ? And what is the best position to declare it?
get_jsoncall.