0

I have an array of JavaScript objects

let data = [
      { type: 'account', title: 'Robb', subscription: 'ESSENTIAL' },
      { type: 'account', title: 'Bran', subscription: 'ESSENTIAL' },
      { type: 'account', title: 'Arya', subscription: 'FREE' },
      { type: 'article', title: 'The Wall' },
      { type: 'account', title: 'Tyrion', subscription: 'ESSENTIAL' },
    ];

The array is already sorted. I want to sort it again by subscription. The requirement is to show ESSENTIAL accounts at the top and leave others as they are. Note that some objects does not have a subscription property.

Vizard
  • 149
  • 1
  • 12

1 Answers1

1

data.sort((a, b) => a.subscription === "ESSENTIAL" ? 1 : -1);

Héctor
  • 24,444
  • 35
  • 132
  • 243