如果您有多个项目,我怎样才能使它显示为 "Fishing Pole (x3)"? [库存命令]

How can I make it where if you have multiple of an item, it shows like "Fishing Pole (x3)"? [Inventory Command]

一个问题,如果你有多个项目,我怎么能把它显示为“鱼竿(3)”? << 将 3 更改为项目数

因为如果嵌入超过最大大小,Discord 将 return 出错。谢谢!

库存代码:

const db = require('quick.db');
const Discord = require('discord.js');

module.exports = {
    name: "inventory",
    description: "View your inventory",


    async run (client, message, args) {
        let items = await db.fetch(message.author.id);
        if(items === null) items = "Nothing"

        const Embed = new Discord.MessageEmbed()
        .addField('Inventory', items)

        message.channel.send(Embed);
    }
}

TL;DR 如何删除重复项并将项数放在项旁边。

您基本上需要将项目数据结构转换为按项目类型组织的其他数据结构。但是我们不知道 'items' 到底是什么。这只是一个可能的例子。假设您的清单是一组对象,您可以执行以下操作:

var items = [{name:'fishing pole'}, {name:'sword'}, {name:'fishing pole'}, {name:'fish'}, {name:'fish'}, {name:'fishing pole'}];
console.log('Disorganized:', items);

var inventoryMap = new Map();

items.forEach(function(item, index, array) {
  var itemArray = inventoryMap.get(item.name);
  if (itemArray) {
    itemArray.push(item);
  } else {
    inventoryMap.set(item.name, [item]);
  }
});

console.log('Sorted:');

inventoryMap.forEach( function(value, key) {
  console.log(key, '('+value.length+')');
});