reactjs 运行for循环时获取不同的数组

9nvpjoqh  于 2022-12-18  发布在  React
关注(0)|答案(1)|浏览(117)
const fetchData = async (e) => {
    
const arr = []
const newres = await axios.get(`https://api.aftership.com/v4/trackings/?tag=${tag}&created_at_min=${minCreate}&created_at_max=${maxCreate}&page=${page}&limit=200`, {
    headers: {
        "Content-Type": "application/json",
        "aftership-api-key": `${apiKey}`
    },
       /*  responseType: "blob" */
})
const res = await newres.data.data.trackings

arr.push(newres.data.data.trackings)
        
const pageNumber = newres.data.data.count/200;
const exactPage = Math.ceil(pageNumber);
      
for(let i=1; i<=exactPage; i++) {
    let newarr = []
    let n = await axios.get(`https://api.aftership.com/v4/trackings/?tag=${tag}&created_at_min=${minCreate}&created_at_max=${maxCreate}&page=${i}&limit=200`, {
        headers: {
           "Content-Type": "application/json",
           "aftership-api-key": `${apiKey}`
        },
    })
    const arrr = n.data.data.trackings
    res.concat(arrr)
    console.log(i);
}

我试图合并所有的数组到1个对象,我们可以下载到CSV,但所有的数组来分开。有人能帮助吗?

oug3syen

oug3syen1#

这是一个如何合并数组的例子:

const fetchData = async () => {
  const arr = [];
  let a = [11, 12, 23];
  for (let i = 0; i < 10; i++) {
    let b = [i,i*7];
    a = a.concat(b);
  }
  console.log(a);
};

我认为您需要在代码中更改:

res.concat(arrr)

致:

res = res.concat(arrr)


并将res从const更改为let
注意:concat()方法用于合并数组。

相关问题