【问题标题】:How can I output specific response in fetch?如何在 fetch 中输出特定响应?
【发布时间】:2021-11-27 04:27:55
【问题描述】:

目前我的下拉列表有大约 30 多个输出,但我想将列表减少到特定项目,但我不知道如何处理。

我的 API 是什么样子的~

{
  "filter": "A",
  "timestamp": "2021-08-05T15:52:02.489962-04:00",
  "contracts": [
    {
      "contractCode": "Code 1",
      "contractName": "Name of contract 1",
      "status": "A"
    },
    {
      "contractCode": "Code 2",
      "contractName": "Name of contract 2",
      "status": "A"
    },
    {
      "contractCode": "Code 3",
      "contractName": "Name of contract 3",
      "status": "A"
    }
  ]
}

假设我只想输出代码 1 和代码 3,如何在我的 fetch 调用中对其进行过滤? 是否可以设置一个 html 复选框来选择我要输出的每个代码?

我的 Fetch 调用

fetch('API LINK')
    .then(response => response.json())
    .then(data => makeContract(data.contracts))
    .catch(error => console.log(`Oh no! ${error}`))
       const makeContract = (contracts) => {
       const body = document.getElementById('contractCode');
       contracts.forEach((contracts) => {
       const htmlTemplate = `
           <option>${JSON.stringify(contracts.contractCode).slice(1, -1)}</option>
        `;
        body.innerHTML += htmlTemplate;
    });
};

【问题讨论】:

    标签: javascript jquery json ajax fetch


    【解决方案1】:

    function demo() {
      const include = Array.from(document.querySelectorAll('[name=code]:checked')).map(e=>e.value);
      const contracts = sample().contracts;
      contracts.forEach(contract => {
        if (include.includes(contract.contractCode)) { console.log(contract); }
      });
    }
    
    function sample() {
      return {
        "filter": "A",
        "timestamp": "2021-08-05T15:52:02.489962-04:00",
        "contracts": [
          {
            "contractCode": "Code 1",
            "contractName": "Name of contract 1",
            "status": "A"
          },
          {
            "contractCode": "Code 2",
            "contractName": "Name of contract 2",
            "status": "A"
          },
          {
            "contractCode": "Code 3",
            "contractName": "Name of contract 3",
            "status": "A"
          }
        ]
      };
    }
    <input type="checkbox" name="code" id="code1" value="Code 1">
    <label for="code1">Code 1</label>
    
    <input type="checkbox" name="code" id="code2" value="Code 2">
    <label for="code2">Code 2</label>
    
    <input type="checkbox" name="code" id="code3" value="Code 3">
    <label for="code3">Code 3</label>
    
    <input type="button" value="Try it" onclick="demo()">

    fetch('API LINK')
      .then(response => response.json())
      .then(data => makeContract(data.contracts))
      .catch(error => console.log(`Oh no! ${error}`));
    
    const makeContract = contracts => {
      const body = document.getElementById('contractCode');
      const include = Array.from(document.querySelectorAll('[name=code]:checked')).map(e=>e.value);
      contracts.forEach(contract => {
        if (!include.includes(contract.contractCode)) { return; }
        const htmlTemplate = `
          <option>${JSON.stringify(contract.contractCode).slice(1, -1)}</option>
        `;
        body.innerHTML += htmlTemplate;
      });
    };
    

    【讨论】:

    • 抱歉打扰了,但我已经尝试过你的方法,但我不确定如何使用 API 链接来实现它:/ 这是我尝试过的:function demo() { const include = Array.from(document.querySelectorAll('[name=code]:checked')).map(e=&gt;e.value); const contracts = sample().contracts; contracts.forEach(contract =&gt; { if (include.includes(contract.contractCode)) { console.log(contract); } }); } function sample() { const selectContract = JSON.stringify(contracts).slice(1, -1) return { selectContract }; }
    • 你想用sample()做什么?我使用它是因为我无法在我的答案中进行提取。但是,contracts 已经存在于您的 makeContract 中。
    • 我正在尝试获取 sample() 中的 JSON,以便过滤它。遗憾的是我无法对其进行硬编码,因为它全部存储在 API 链接中我什至尝试在 sample() function sample() { $.ajax(test).done(function (response) { console.log(response); return { response }; }); } 中使用 ajax
    • 你为什么不简单地将我的行添加到makeContract并稍微修改一下?
    • 我已经尝试过了,但它返回错误Array.forEach (&lt;anonymous&gt;)
    猜你喜欢
    • 2019-09-21
    • 2016-04-03
    • 2022-12-13
    • 2017-01-16
    • 2017-02-09
    • 2017-01-04
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多