【问题标题】:How to from array want to form a pattern to make a query to make API call如何从数组中想要形成一个模式来进行查询以进行 API 调用
【发布时间】:2021-07-02 22:20:02
【问题描述】:

我想从一个数组中导出一个模式。数组可以是n 元素个数

这是我从 DB 收到的数组模式,(注意这里的元素可能是 n 数字)

[
  { id: '2', name: 'ONe' },
  { id: '3', name: 'Twop' },
  { id: '1', name: 'ThreeC' }
]

我想要一个像AccountId=2&AccountId=3&AccountId=1 这样的模式,由数组和id 组成

我想将形成的数据作为查询参数传递到下面的 URL 中以进行 API 调用。

 const config = {
          method: 'get',
          url: `${URL}api/cost?AccountId=1&AccountId=2&AccountId=3`,
          headers: {
            'Cookie': 'ARRAffinity=6f6eb54d3b6d7ed13173b9203b0bd6571b611d626818fba77a815805a7c90146'
          },
          data: data
        };
        const dataOutput = await axios(config )
        .then(function (response) {
          console.log(JSON.stringify(response.data));
          return response.data;
        })
        .catch(function (error) {
          console.log(error);
        });

【问题讨论】:

  • AccountId 是否等同于 name

标签: javascript node.js arrays api axios


【解决方案1】:

使用mapjoin 构建参数字符串

const data = [
  { id: "2", name: "ONe" },
  { id: "3", name: "Twop" },
  { id: "1", name: "ThreeC" },
];

const params = data.map(({ id }) => `AccountId=${id}`).join("&");
const url = `foo.com/api/cost?${params}`;

console.log(url);

【讨论】:

    【解决方案2】:

    你可以这样做:

    stackOverflow = () => {
        let requestPartStr = '';
        const data = [
            { id: '2', name: 'ONe' },
            { id: '3', name: 'Twop' },
            { id: '1', name: 'ThreeC' }
        ];
        data.forEach((val, index) => {
            requestPartStr += index !== data.length - 1 
                ? `AccountId=${val.id}&` 
                : `AccountId=${val.id}`;
        })
    
        return requestPartStr;
    };
    

    根据给定的代码,这可以是一个完整的代码示例:

    const data = [
        { id: '2', name: 'ONe' },
        { id: '3', name: 'Twop' },
        { id: '1', name: 'ThreeC' }
    ];
    
    getRequestParameters = (data) => {
        let requestPartStr = '';
        
        data.forEach((val, index) => {
            requestPartStr += index !== data.length - 1
                ? `AccountId=${val.id}&`
                : `AccountId=${val.id}`;
        })
    
        return requestPartStr;
    };
    
    const requestParameters = getRequestParameters(data);
    const config = {
        method: 'get',
        url: `${URL}api/cost?${requestParameters}`,
        headers: {
            'Cookie': 'ARRAffinity=6f6eb54d3b6d7ed13173b9203b0bd6571b611d626818fba77a815805a7c90146'
        },
        data: data
    };
    const dataOutput = await axios(config)
    .then(function (response) {
        console.log(JSON.stringify(response.data));
        return response.data;
    })
    .catch(function (error) {
        console.log(error);
    });
    

    【讨论】:

    • 如果您想用打字来换取可读性,可以联系data.map(o=>`AccountId=${o.id}`).join('&');
    猜你喜欢
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 2021-02-07
    • 2019-10-05
    • 1970-01-01
    • 2013-01-06
    相关资源
    最近更新 更多