Javascript 我正试图抢夺亚马逊,并在一个特定的数字上停下来
这是我的代码Javascript 我正试图抢夺亚马逊,并在一个特定的数字上停下来,javascript,discord,discord.js,bots,increment,Javascript,Discord,Discord.js,Bots,Increment,这是我的代码 if(body.included!=null&&body.included.length>0){ 让体裁=“”; for(设i=0;i
if(body.included!=null&&body.included.length>0){
让体裁=“”;
for(设i=0;i
这是每当我用它搜索任何东西时的结果,它会给我以下信息:
Comedy, Kids, Fantasy, Fantasy World, Erotic Torture, Loli, Nudity, Bdsm, Bondage, Sex, Past, Plot Continuity, Violence, Military, Mecha, Historical, Action, Romance, Science Fiction, World War II, Japan, Asia, Piloted Robot, Alternative Past, Steampunk, Gunfights, Alien, War, Robot, Adventure, Space Travel, Cyborg, Crime, Other Planet, Humanoid Alien, Future, Space, Contemporary Fantasy, Vampire, Slice of Life, Detective, Bounty Hunter, Magic, Present, Demon, Super Power, Drama, Anime Influenced, Earth, Love Polygon, Angst, High School, School Life
这是一个例子,因为其他类型的搜索有1个或2个或相当数量的类型,而它没有40个这样的类型
像这个
Ninja, Fantasy World, Adventure, Action, Comedy, Martial Arts, Super Power, Romance, Disaster, Shounen, Love Polygon, Angst, Plot Continuity, Parallel Universe, Fantasy
因此,我需要帮助的是,如何使它停止在某个特定的数字中,而不会给我40个,而不是10个或更少您可以更改循环条件,但仍然需要注意
主体的长度。对于少于10个元素的情况,包括数组。请尝试以下操作:
const MAX_GENRES = 10;
if (body.included && body.included.length) {
const max = Math.min(MAX_GENRES, body.included.length);
const genres = [];
let i = 0;
while (i < max) {
genres.push(body.included[i].attributes.title);
i += 1;
}
embed.addField('GENRES', [genres.join(',')], true);
}
您是否尝试过更改for循环条件?:for(让i=0;i<10;i++)
embed.addField('GENRES', genres, true);