【问题标题】:Can't get the last array in javascript无法在javascript中获取最后一个数组
【发布时间】:2021-05-09 22:38:14
【问题描述】:
const search = await spot.search(request, {
  limit: 3,
  type: ['track']
});
search.tracks.items.forEach(song => {
  let tableArtists = []
  song.artists.forEach(artist => {
    if (song.artists[0].name == artist.name) return tableArtists.push(artist.name)
    if (song.artists[song.artists.length].name == artist.name) return tableArtists.push(" & " + artist.name)
    tableArtists.push(", " + artist.name)
  })
  console.log(song.name + " - " + tableArtists.join(" "))
})

这个代码应该得到一首歌曲中的所有艺术家,最后一个以&符号开头。目前,它不会得到最后一位艺术家的名字。为什么?仅供参考:song.artists.last() 也不起作用。

【问题讨论】:

标签: javascript node.js


【解决方案1】:

song.artists 数组的最后一个元素的索引不是song.artists.length 而是song.artists.length - 1

替换

if (song.artists[song.artists.length].name == artist.name) return tableArtists.push(" & " + artist.name)

if (song.artists[song.artists.length - 1].name == artist.name) return tableArtists.push(" & " + artist.name)

【讨论】:

    【解决方案2】:

    搜索响应:

    Track {
      availableMarkets:
       [ 'AD',
         'AE',
         'AG',
         'AL',
         'AM',
         'AO',
         'AR',
         'AT',
         'AZ',
         'BA',
         'BB',
         'BD',
         'BF',
         'BG',
         'BH',
         'BI',
         'BJ',
         'BN',
         'BO',
         'BR',
         'BS',
         'BW',
         'BY',
         'BZ',
         'CA',
         'CH',
         'CI',
         'CL',
         'CM',
         'CO',
         'CR',
         'CV',
         'CW',
         'CY',
         'CZ',
         'DE',
         'DJ',
         'DK',
         'DM',
         'DO',
         'DZ',
         'EC',
         'EE',
         'EG',
         'FI',
         'FJ',
         'FM',
         'FR',
         'GA',
         'GB',
         'GD',
         'GH',
         'GM',
         'GN',
         'GQ',
         'GR',
         'GT',
         'GW',
         'HK',
         'HN',
         'HR',
         'HT',
         'HU',
         'ID',
         'IE',
         'IL',
         'IN',
         'IS',
         'IT',
         'JM',
         'JO',
         'JP',
         'KE',
         'KG',
         'KH',
         'KM',
         'KN',
         'KR',
         'KW',
         'KZ',
         'LA',
         'LB',
         'LC',
         'LK',
         'LR',
         'LS',
         'LT',
         'LV',
         'MA',
         'MC',
         'MD',
         'ME',
         'MG',
         'MK',
         'ML',
         'MN',
         'MO',
         'MR',
         'MT',
         'MU',
         ... 55 more items ],
      discNumber: 1,
      duration: 165440,
      explicit: false,
      externalUrls:
       { spotify: 'https://open.spotify.com/track/483XiZ5o13Cc1zoWV7jGml' },
      href: 'https://api.spotify.com/v1/tracks/483XiZ5o13Cc1zoWV7jGml',
      id: '483XiZ5o13Cc1zoWV7jGml',
      name: 'Gecko (Overdrive) - Radio Edit',
      previewUrl:
       'https://p.scdn.co/mp3-preview/9ed94074e04a78652aecf9f4c58ebffe9cd16fbd?cid=ac37a89f1e77422e955b85f07d8a6cdb',
      trackNumber: 1,
      type: 'track',
      uri: 'spotify:track:483XiZ5o13Cc1zoWV7jGml',
      playable: undefined,
      local: false,
      externalIds: { isrc: 'CYA111400063' },
      popularity: 70,
      restrictions: null }
    

    【讨论】:

    • 这不是答案。任何人都可以使用它来测试您的代码,这也不是有效的 javascript。使用有效的响应数据编辑问题并删除此答案。您还可以删除不相关的属性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    相关资源
    最近更新 更多