How to sort data by price in an array with the same field and remove the smallest from the array?

  • 0
    Good day.
    It is not possible to sort the data by price in an array with the same field and remove the smallest one from the array
    [{ name: "Creeper's Cruel Painsaw", price: '2.53', rarity: 'Common' },
    { name: "Creeper's Cruel Painsaw", price: '2.50', rarity: 'Common' },
    { name: 'Cuffs of Oak and Yew', price: '3.94', rarity: 'Rare' },
    { name: 'Cuffs of Oak and Yew', price: '4.99', rarity: 'Rare' },
    { name: 'Curled Root-Staff', price: '2.91', rarity: 'Common' },
    { name: 'Curled Root-Staff', price: '3.33', rarity: 'Common' }]
    JavaScript Leah Key, Jul 30, 2020

  • 1 Answers
  • 0
    const arr = [{ name: "Creeper's Cruel Painsaw", price: '2.53', rarity: 'Common' },
    { name: "Creeper's Cruel Painsaw", price: '2.50', rarity: 'Common' },
    { name: 'Cuffs of Oak and Yew', price: '3.94', rarity: 'Rare' },
    { name: 'Cuffs of Oak and Yew', price: '4.99', rarity: 'Rare' },
    { name: 'Curled Root-Staff', price: '2.91', rarity: 'Common' },
    { name: 'Curled Root-Staff', price: '3.33', rarity: 'Common' }];

    const sorted = [...arr].sort((a, b) => a.price - b.price).slice(1);
    console.log(sorted); /* [
    { name: "Creeper's Cruel Painsaw", price: "2.53", rarity: "Common" },
    { name: "Curled Root-Staff", price: "2.91", rarity: "Common" },
    { name: "Curled Root-Staff", price: "3.33", rarity: "Common" },
    { name: "Cuffs of Oak and Yew", price: "3.94", rarity: "Rare" },
    { name: "Cuffs of Oak and Yew", price: "4.99", rarity: "Rare" }
    ] */
    Anonymous

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