How to remove the first element of an array based on its name?

  • 0
    [{ name: 'Basher of Mage Skulls', price: '78.00', rarity: 'Immortal' },
      { name: 'Basher of Mage Skulls', price: '78.85', rarity: 'Immortal' },
      { name: 'Basher of Mage Skulls', price: '78.99', rarity: 'Immortal' },
      { name: 'Basher of Mage Skulls', price: '79.15', rarity: 'Immortal' },
      { name: 'Basher of Mage Skulls', price: '79.99', rarity: 'Immortal' },
      { name: 'Basher of Mage Skulls', price: '79.99', rarity: 'Immortal' },
      { name: 'Basher of Mage Skulls', price: '80.00', rarity: 'Immortal' },
      { name: 'Basher of Mage Skulls', price: '80.14', rarity: 'Immortal' },
      { name: 'Basher of Mage Skulls', price: '81.72', rarity: 'Immortal' },
      { name: 'Basher of Mage Skulls', price: '83.62', rarity: 'Immortal' },
      { name: 'Basher of Mage Skulls', price: '83.62', rarity: 'Immortal' },
      { name: 'Basher of Mage Skulls', price: '84.36', rarity: 'Immortal' },
      { name: 'Basher of Mage Skulls', price: '86.99', rarity: 'Immortal' },
      { name: 'Basher of Mage Skulls', price: '88.50', rarity: 'Immortal' },
      { name: 'Basher of Mage Skulls', price: '90.00', rarity: 'Immortal' },
      { name: 'Basher of Mage Skulls', price: '95.00', rarity: 'Immortal' },
      { name: 'Demon Eater', price: '1499.95', rarity: 'Arcana' },
      { name: 'Demon Eater', price: '1553.55', rarity: 'Arcana' },
      { name: 'Demon Eater', price: '1553.55', rarity: 'Arcana' },
      { name: 'Demon Eater', price: '2900.00', rarity: 'Arcana' },
      { name: 'Demon Eater', price: '2900.00', rarity: 'Arcana' }]


    How can I remove all elements from such an array except the first (There is an object - Demon Eater, it needs to remove all subsequent elements, and leave the very first one. Or just write the first element into another array. And so on with each name.)
    JavaScript Levi Bauer, Apr 9, 2019

  • 2 Answers
  • 0
    [...].filter(({name}, i, arr) => !i || name !== arr[i-1].name)
    Mia Larson

  • 0
    const data = [
    { name: "Basher of Mage Skulls", price: "78.00", rarity: "Immortal" }
    // etc ...
    ];

    const filtered = Object.values(
    data.reduce((acc, val) => ((acc[val.name] ||= val), acc), {})
    );
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!