Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 139953

Javascript Array Specific Order

$
0
0

I have an array of js object

const name = 'Levi'
var data = [ 
{ firstname: 'Levi', food: 'apple'     },
{ firstname: 'Eren', food: 'orange'   },
{ firstname: 'Levi', food: 'grapes' },
{ firstname: 'Miks', food: 'banana' }
];

How can I sort the firstname and prioritized (Levi) as on top.

Expected Output:

[{firstname: Levi, food: apple},
{ firstname: Levi, food: grapes },
{ firstname: Eren, food: orange   },
{ firstname: Miks, food: banana}]

I used this code

this.data.sort((a, b) => (a.firstname > name) ? 1 : ((name> a.firstname) ? -1 : 0));

Viewing all articles
Browse latest Browse all 139953

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>