我怎样才能从这个数组中得到 "title"?
How can i get "title" from this array?
代码///
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(数据)我正在获取此数组:
[{"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 的描述并将其post 添加到 addfield?
const parsedData = JSON.parse(data);
const descriptions = parsedData.map(item => item.description)
[...].addField('desc', JSON.stringify(descriptions))
您可以阅读更多关于 map
的行为 here
编辑:您需要 JSON.stringify
因为 desc
必须是字符串,而不是数组。
代码///
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(数据)我正在获取此数组:
[{"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 的描述并将其post 添加到 addfield?
const parsedData = JSON.parse(data);
const descriptions = parsedData.map(item => item.description)
[...].addField('desc', JSON.stringify(descriptions))
您可以阅读更多关于 map
的行为 here
编辑:您需要 JSON.stringify
因为 desc
必须是字符串,而不是数组。