如何在使用 Axios 时从数组中获取字符串

How to Get String from Array while using Axios

我正在使用 axios,我想从这个 api https://api.dictionaryapi.dev/api/v2/entries/en/WORD_THAT_I_WANT_TO_SEARCH 中获取“单词和定义” 这是我当前的代码:

let input = prompt("Enter !meaning of /YOUR WORD");
if (input.toLowerCase().startsWith("!meaning of")) {
                searchOutput = input.split("/");
                axios.get('https://api.dictionaryapi.dev/api/v2/entries/en/' + searchOutput[1])
                    .then(response => {
                    console.log("Meaning of " + searchOutput[1] + "\n\n\n" + response.data.join('\n'));
                    })
                    .catch(error => {
                    console.log(error);
                });
            }

下面的代码在一行中给出了定义:更改我使用的分隔符 --OR--

  • 您将在由 --OR--
  • 分隔的单行中得到结果
let input = prompt("Enter !meaning of /YOUR WORD"); // take input

if (input.toLowerCase().startsWith("!meaning of")) {
    const searchOutput = input.split("/");
    axios.get('https://api.dictionaryapi.dev/api/v2/entries/en/' + searchOutput[1])
        .then(response => {
            let definitions = ""
            console.log('Meaning of the ' + response.data[0].word + ' is:')
            for (let i = 0; i < response.data[0].meanings.length; i++) {
                let res = response.data[0].meanings[i].definitions[0].definition
                definitions += res + " --OR-- ";
            }
            console.log(definitions)
        })
        .catch(error => {
            console.log(error);
        });
}

检查控制台中响应对象的输出(来自 axios 输出)并从中获取您需要的数据。

输出:

  • 查看下面的图像以检查输出 prompt result

如果您的要求得到满足,请告诉我