如何将两个单独的JavaScript Axios结果集合并?

huangapple go评论61阅读模式
英文:

How can I combine two separate JavaScript Axios result sets?

问题

我对JavaScript不太熟悉,但我有一个可以使用Axios查询一系列值的工作程序。该程序旨在查看一段时间范围内的信息,但网站每次只返回一个月的数据,而不是几个月的完整数据集。我可以为每个月执行查询,但我不确定如何将这些Axios结果组合起来,以获取我想要的完整数据集。

在下面的示例中,我想要组合data1和data2。Concat不起作用,所以我想知道如何组合这两个Axios结果。

const axios = require('axios');

async function getdata(urlString) {
    try {
        const response = await axios.get(urlString);
        //const response = await axios.get(urlString);
    } catch (error) {
        console.error(error);
    }
}

const urlString1 = 'https://www.recreation.gov/api/permits/233393/availability/month?start_date=2023-07-01T00:00:00.000Z&commercial_acct=false&is_lottery=false';
const urlString2 = 'https://www.recreation.gov/api/permits/233393/availability/month?start_date=2023-07-01T00:00:00.000Z&commercial_acct=false&is_lottery=false';

const data1 = getdata(urlString1);
const data2 = getdata(urlString2);

如何组合这两个Axios结果?

英文:

I'm not well versed in Javascript but have a working program that uses Axios to query a range of values. The program was written to look at a date range of information but the website only returns one month's worth of data at a time vs. a full set of several months. I can do a query for each month but I'm not sure how to combine the axis results to have a full data set for the time range I want.

In the example below I'd like to combine data1 and data2. Concat doesn't work so I'm looking to know how I can combine these two Axios results. (

const axios = require('axios');


async function getdata(urlString) {
    try {
      const response = await axios.get(urlString);
      //const response = await axios.get(urlString);
} catch (error) {
      console.error(error);
    }
}

     const urlString1 = 'https://www.recreation.gov/api/permits/233393/availability/month?  start_date=2023-07-01T00:00:00.000Z&commercial_acct=false&is_lottery=false';
     const urlString2 = 'https://www.recreation.gov/api/permits/233393/availability/month?start_date=2023-07-01T00:00:00.000Z&commercial_acct=false&is_lottery=false';
    const data1 = getdata(urlString1);


    const data2 = getdata(urlString2);

答案1

得分: 1

以下是您需要的内容:

const data1 = await getdata(urlString1);
const data2 = await getdata(urlString2);
let finalResult = { ...data1, ...data2 };
英文:

Here is what you need:

const data1 = await getdata(urlString1);
const data2 = await getdata(urlString2);
let finalResult={...data1,...data2};

huangapple
  • 本文由 发表于 2023年6月29日 10:58:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定