我如何从这个阵列中获得“头衔”?

时间:2018-04-04 19:06:38

标签: node.js discord discord.js

代码///

  https.get(`https://discordemoji.com/api?request=search&q=coffee}`, (resp) => {
let data = '';

resp.on('data', (chunk) => {
  data += chunk;
});

resp.on('end', () => {
  console.log(data)
  const embed = new Discord.RichEmbed()
  .setTitle(`Search result for: "coffee}"`)
  .addField(`desc`, `link`)
  message.channel.send({embed});
});

  }).on("error", (err) => {
    console.log("Error: " + err.message);
  });
来自console.log(data)的

我得到这个数组:

[{"id":1857,"title":"Coffee","slug":"Coffee","description":"Good day!","category":1,"faves":4,"submitted_by":"Cristy","did":"339752841612623872"},{"id":432,"title":"AzuCoffee","slug":"AzuCoffee","description":"AzuCoffee is an anime style emoji","category":4,"faves":1,"submitted_by":"Kohai","did":"116218776495587329"},{"id":340,"title":"FeelsCoffeeMan","slug":"FeelsCoffeeMan","description":"FeelsCoffeeMan is a custom pepe style emoji","category":3,"faves":2,"submitted_by":"Kohai","did":"116218776495587329"}]

如何获取数组中每个id的描述并将其发布到addfield?

1 个答案:

答案 0 :(得分:0)

const parsedData = JSON.parse(data);

const descriptions = parsedData.map(item => item.description)

[...].addField('desc', JSON.stringify(descriptions))

您可以详细了解map的行为here

编辑:您需要JSON.stringify,因为desc必须是字符串,而不是数组。