数组未从函数 [duplicate] 返回推送值

Array not returning pushed values from Function [duplicate]

提问人:nb_nb_nb 提问时间:8/25/2023 更新时间:8/25/2023 访问量:22

问:

我在函数外部声明一个数组,并在函数内部推送值。如何在函数外部填充该数组?

我的代码:

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返回:

"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"
                    }
                }
]

我想返回:myArr

[
  {"id": "123", "nm": "Harry Potter"},
  {"id": "456", "nm": "Percy Jackson"},
  {"id": "890", "nm": "Game of Thrones"},
  {"id": "678", "nm": "Lord of the Rings"},
]

目前,我的代码刚刚返回.如何填充?[]myArr

JavaScript 数组 对象 ECMASCRIPT-6 AXIOS

评论


答: 暂无答案