How to change .map function order (asc & desc) in javascript?

Viewed 48

I have total 25 song list in spotify, it may increase in future. Like 1, 2, 3, 4 ..... 25. In my case the map() function show only top 10 song (1 to 10). when a new song add in my playlist it got position from 26th number like 26, 27, 28 ..... n value.

Now, how I can order my map() function? I want to show 25, 24, 23, 22.... like (Z to A)

Anyone can help me?

  const tracks = items.slice(0, 10).map((track: ResponseTrackType) => ({
    artist: track.artists.map((_artist) => _artist.name).join(', '),
    songUrl: track.external_urls.spotify,
    cover: track.album.images[1]?.url,
    title: track.name
  }));

I want to replace slice() to ascending, descending order.

1 Answers

Javascript has a reverse() method that you can call in an array

const tracks = items.reverse().slice(0, 10).map((track: ResponseTrackType) => ({
  artist: track.artists.map((_artist) => _artist.name).join(', '),
  songUrl: track.external_urls.spotify,
  cover: track.album.images[1]?.url,
  title: track.name
}));

from this the elements are taken from reversed order, ie recently added order.last added will be the first. There is also one library called underscore.js(_.js). you can also use it

Related