Home > Back-end >  How to Get String from Array while using Axios
How to Get String from Array while using Axios

Time:02-03

Im using axios and I want to get the "word and definitions" from this api https://api.dictionaryapi.dev/api/v2/entries/en/WORD_THAT_I_WANT_TO_SEARCH here is my current code:

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);
                });
            }

CodePudding user response:

The below piece of code gives you the definitions in a single line: change the deliminator I have used --OR--

  • You will get the result in a single line separated by --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);
        });
}

Check the output of the response object (from axios output) in the console and grab the data you require from it.

Output:

  • See the following images for checking the output prompt result

Let me know if your requirement is satisfied or not

  •  Tags:  
  • Related