简体   繁体   English

如何使用 Javascript 更改 API 响应

[英]How to alter a API response with Javascript

I wrote this little app that takes in a set of starwars characters and returns images in an array.我编写了这个小应用程序,它接收一组星球大战字符并以数组形式返回图像。

const app = require('express')(); 
const fetch = require('node-fetch');
const imageSearch = require('image-search-google');
const bodyParser = require('body-parser')

var urlencodedParser = bodyParser.urlencoded({ extended: true })
app.use(bodyParser.json())


const options = {page:1};


const getImages = (keywords) =>
 Promise.all(keywords.map(keyword => client.search(`${keyword} Wookieepedia`, options)))
 .then(data => firstResult = data.map(result => result[0].url)); 


 const fun = async () => {
     const res = await getImages(['yoda' , 'luke skywalker' , 'darth vader']);
     console.log(res);
 }

 fun()

The result of the above code looks like this :上面代码的结果如下所示:

[ 'https://vignette.wikia.nocookie.net/starwars/images/d/d6/Yoda_SWSB.png/revision/latest?cb=20150206140125',
  'https://vignette.wikia.nocookie.net/starwars/images/d/d9/Luke-rotjpromo.jpg/revision/latest?cb=20091030151422',
  'https://vignette.wikia.nocookie.net/starwars/images/a/a3/ANOVOS_Darth_Vader_1.png/revision/latest?cb=20150128225029' ]

But i would like a result where i can know which image belongs to which keyword.但我想要一个结果,我可以知道哪个图像属于哪个关键字。 Maybe a format like this or something similar :也许是这样的格式或类似的格式:

[{<keyword> : <url>}]

I tried this :我试过这个:

const getImages = (keywords) =>
 Promise.all(keywords.map(keyword => {return { [keyword] : [client.search(`${keyword} Wookieepedia`, options)]}    } ))
 .then(data => firstResult = data.map(result => result)); 


 const fun = async () => {
     const res = await getImages(['yoda' , 'luke skywalker' , 'darth vader']);
     console.log(res);
 }

 fun()

Results were close but not good :结果接近但不好:

[ { yoda: [ [Promise] ] },
  { 'luke skywalker': [ [Promise] ] },
  { 'darth vader': [ [Promise] ] } ]

client.search returns a Promise. client.search返回一个 Promise。 It can be awaited inside the mapping function if you make the mapping function async.如果您使映射函数异步,则可以在映射函数内等待它。

const getImages = (keywords) =>
    Promise.all(keywords.map(async keyword => {
        return {[keyword]: await client.search(`${keyword} Wookieepedia`, options)}
    }));
const getImages = (keywords) =>
 Promise.all(keywords.map(keyword => client.search(`${keyword} Wookieepedia`, options).then(result => [keyword, result])))
 .then(entries => Object.fromEntries(entries))
 .then(data => firstResult = data.map(result => result)); 

 const fun = async () => {
     const res = await getImages(['yoda' , 'luke skywalker' , 'darth vader']);
     console.log(res);
 }

 fun()

Without using async you could add a then clause to the search promise that combines the keyword value with the result array - or with the first result as shown:在不使用async您可以在搜索承诺中添加一个then子句,将关键字值与结果数组相结合——或者与第一个结果相结合,如下所示:

const getImages = keywords => Promise.all(
     keywords.map( keyword =>
         client.search(`${keyword} Wookieepedia`, options)
        .then( result => ( {[keyword]: result[0]} ))
     )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM