在定义对象键值时变得不确定

时间:2020-03-23 20:57:38

标签: javascript arrays object

var playlistResponse = {
  "id": "59ZbFPES4DQwEjBpWHzrtC",
  "followers": {
    "href": null,
    "total": 143350
  },
  "trackInfo": {
    "playlistType": "favorites",
    "lastUpdated": "2015-11-26T19:13:31",
    "tracks": [
      {
        "id": "0C0XlULifJtAgn6ZNCW2eu",
        "type": "track",
        "artist": "Travis Scott",
        "title": "goosebumps",
        "length": "4:03",
        "playCount": 404489462,
        "genre": "rap"
      },
      {
        "id": "4OHNH3sDzIxnmUADXzv2kT",
        "type": "track",
        "artist": "LCD Soundsystem",
        "title": "Someone Great",
        "length": "6:30",
        "playCount": 23765430,
        "genre": "alternative"
      },
      {
        "id": "3twNvmDtFQtAd5gMKedhLD",
        "type": "track",
        "artist": "Cut Copy",
        "title": "Hearts On Fire",
        "length": "4:52",
        "playCount": 23098109,
        "genre": "alternative"
      },
      {
        "id": "5m4VYOPoIpkV0XgOiRKkWC",
        "type": "track",
        "artist": "Cut Copy",
        "title": "HUMBLE.",
        "length": "4:11",
        "playCount": 768356720,
        "genre": "rap"
      },
      {
        "id": "1l63szZeUpN1m87MOD1u7K",
        "type": "track",
        "artist": "Whitney Houston",
        "title": "I Wanna Dance With Somebody (Who Loves Me)",
        "length": "4:51",
        "playCount": 184656875,
        "genre": "pop"
      }
    ]
  }
};

这是我试图从中提取信息的对象。我正在尝试获取包含titlelength列表的对象。这是我当前的代码和输出

function findAllLonger(playListInput, trackTime) {
  var listOfTracks = {};
  var arrayOfTracks = playListInput.trackInfo.tracks;

  arrayOfTracks.forEach(function (value) {
    listOfTracks[value.title] = listOfTracks[value.length];
  })

  return listOfTracks;
}

/* Output
{ goosebumps: undefined,
  'Someone Great': undefined,
  'Hearts On Fire': undefined,
  'HUMBLE.': undefined,
  'I Wanna Dance With Somebody (Who Loves Me)': undefined }
*/

我不明白为什么我似乎无法访问我的length属性。它返回undefined,但是如果我要运行console.log(listOfTracks[0].length),我将获得一个已定义的值,但是此代码应与forEach循环中的代码等效。我在这里想念什么?

1 个答案:

答案 0 :(得分:1)

我认为您想做的是:

var playlistResponse = {
  "id": "59ZbFPES4DQwEjBpWHzrtC",
  "followers": {
    "href": null,
    "total": 143350
  },
  "trackInfo": {
    "playlistType": "favorites",
    "lastUpdated": "2015-11-26T19:13:31",
    "tracks": [
      {
        "id": "0C0XlULifJtAgn6ZNCW2eu",
        "type": "track",
        "artist": "Travis Scott",
        "title": "goosebumps",
        "length": "4:03",
        "playCount": 404489462,
        "genre": "rap"
      },
      {
        "id": "4OHNH3sDzIxnmUADXzv2kT",
        "type": "track",
        "artist": "LCD Soundsystem",
        "title": "Someone Great",
        "length": "6:30",
        "playCount": 23765430,
        "genre": "alternative"
      },
      {
        "id": "3twNvmDtFQtAd5gMKedhLD",
        "type": "track",
        "artist": "Cut Copy",
        "title": "Hearts On Fire",
        "length": "4:52",
        "playCount": 23098109,
        "genre": "alternative"
      },
      {
        "id": "5m4VYOPoIpkV0XgOiRKkWC",
        "type": "track",
        "artist": "Cut Copy",
        "title": "HUMBLE.",
        "length": "4:11",
        "playCount": 768356720,
        "genre": "rap"
      },
      {
        "id": "1l63szZeUpN1m87MOD1u7K",
        "type": "track",
        "artist": "Whitney Houston",
        "title": "I Wanna Dance With Somebody (Who Loves Me)",
        "length": "4:51",
        "playCount": 184656875,
        "genre": "pop"
      }
    ]
  }
};

function findAllLonger(playListInput, trackTime) {
  var listOfTracks = {};
  var arrayOfTracks = playListInput.trackInfo.tracks;

  arrayOfTracks.forEach(function (value) {
    listOfTracks[value.title] = value.length;
  })

  return listOfTracks;
}

console.log(findAllLonger(playlistResponse))

/* Output
{ goosebumps: '4:03',
  'Someone Great': '6:30',
  'Hearts On Fire': '4:52',
  'HUMBLE.': '4:11',
  'I Wanna Dance With Somebody (Who Loves Me)': '4:51' }
*/

您正在尝试访问listOfTracks上的属性“ 6:30”(例如)。