分解复杂对象

时间:2019-10-22 22:56:06

标签: javascript object destructuring

我正在尝试破坏一个复杂的对象,但是在控制台中没有任何输出。我不明白为什么。我总是在浏览器中得到某种Uncaught SyntaxError。我试图找出我在这里做错了什么。 (编辑:我仅尝试将“命中”记录到浏览器控制台中,而不是在浏览器中实际打印它们)这是代码:

let catalog = {
  "artists": [
    {
      name: "Journey",
      "hits": [
        "Faithfully",
        "Only the Young",
        "Dont Stop Believing"
      ],
      name: "REO Speedwagon",
      hits: [
        "Keep On Loving You",
        "Time for Me to Fly",
        "Cant Fight This Feeling"
      ],
      name: "Styx",
      hits: [
        "Come Sail Away",
        "Mr. Roboto",
        "Blue Collar Man"
      ]
    }
  ]
};

let { hits } = catalog.artists;

for (x in hits) {
  console.log(hits[x]);
};

3 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,则您只是在尝试打印每位歌手的热门歌曲列表。在这种情况下,我将执行以下操作:

    let catalog = {
    "artists": [
      {
        name: "Journey",
        hits: [
          "Only the young",
          "Dont Stop"
        ]
      },
      {
        name: "Styx",
        hits: [
          "Come Sail Away",
          "Mr. Roboto"
        ]
      }
    ]
  }

  let artistsArr = catalog.artists;

  function printHits() {
    for (var i = 0; i < artistsArr.length; i++) {
      console.log(artistsArr[i].hits.map(hit => hit))
    }
  }

这将映射每个艺术家,并在您调用printHits()时打印他们的歌曲列表。

答案 1 :(得分:0)

此代码比第一个热门歌曲中的字符“更多”是“更多”的问题:)我认为他希望与歌手和歌手的歌曲有一系列对象。 ,tracklen等都丢失了!!)

            let catalog = {
            "artists": [{
                name: "Journey",
                hits: ["Faithfully", "Only the Young", "Dont Stop Believing"],
            }, {
                name: "REO Speedwagon",
                hits: ["Keep On Loving You", "Time for Me to Fly", "Cant Fight This Feeling"]
            }, {
                name: "Styx",
                hits: ["Come Sail Away", "Mr. Roboto", "Blue Collar Man"]

            }]
        };

在这里,我们的音乐爱好者很懒惰。否则,他会意识到上面的所有错误:)

        for (var i = 0; i < catalog.artists.length; i++) {
          var hits = catalog.artists[i];
          console.log(hits.name);
          for (var j = 0; j < catalog.artists[i].hits.length; j++) {
            console.log("               "+catalog.artists[i].hits[j]);
          }

        }

将导致:

Journey 
          Faithfully
          Only the Young
          Dont Stop Believing
REO Speedwagon
           Keep On Loving You
           Time for Me to Fly
           Cant Fight This Feeling
Styx
           Come Sail Away
           Mr. Roboto
           Blue Collar Man

并不是每时每刻都需要快捷方式,快速甚至一个绝妙的主意来显示您的状态如何,在这种情况下它让您感到困惑,因为您在调试时看不到中间的任何步骤。 至少音乐的味道还不错:)玩得开心!

答案 2 :(得分:0)

我想我理解你的问题。 “热门”并非直接嵌套在艺术家下方。 “热门”是艺术家对象的数组。我只是使用Array.map()从“ catalog.artists”数组中删除“ hits”数组。

var catalog = {
  artists: [{
      name: "Journey",
      hits: [
        "Faithfully",
        "Only the Young",
        "Dont Stop Believing"
      ],
    },
    {
      name: "REO Speedwagon",
      hits: [
        "Keep On Loving You",
        "Time for Me to Fly",
        "Cant Fight This Feeling"
      ],
    },
    {
      name: "Styx",
      hits: [
        "Come Sail Away",
        "Mr. Roboto",
        "Blue Collar Man"
      ]
    },
  ]
};

var hits = catalog.artists.map(x => x.hits);

console.log(hits);