I can't seem to figure out how to return a array from a JSON object. I have an object and I am trying to take the name property from the JSON object and put it into a global array called: itemNames =[];
This array is going to be used as arguments for another function.
var itemNames = [];
var itemsIds =[];
function itemArray() {
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(myJson) {
let itemNames =[];
let itemsIds =[];
Object.values(myJson).forEach(e=> {
itemNames.push(e.name);
itemsIds.push(e.id);
});
})
return itemNames;
}
itemArray();
console.log(itemNames);
Expected output:
itemNames =[item1, item2, item3]