I declare an array outside of a function and push values inside the function. How do I get that array populated outside the function?
My code:
const higher_fnc = (query_name) => {
return axios({
url: GraphQL_Endpoint,
method: "post",
headers: headers,
data: query_name,
});
};
let myArr = [];
higher_fnc({
query: query1,
variables: {
id: "1234567",
},
}).then((d) => {
for (const res of d.data) {
const myObj = {};
myObj["id"] = res.node.id;
myObj["nm"] = res.node.title;
myArr.push(myObj);
}
});
console.log(myArr);
res
returns:
"data": [
{
"node": {
"id": "123",
"title": "Harry Potter"
}
},
{
"node": {
"id": "456",
"title": "Percy Jackson"
}
},
{
"node": {
"id": "890",
"title": "Game of Thrones"
}
},
{
"node": {
"id": "678",
"title": "Lord of the Rings"
}
}
]
I want myArr
to return:
[
{"id": "123", "nm": "Harry Potter"},
{"id": "456", "nm": "Percy Jackson"},
{"id": "890", "nm": "Game of Thrones"},
{"id": "678", "nm": "Lord of the Rings"},
]
Currently, my code is just returning []
. How do I get myArr
populated?