【问题标题】:Giving a name to the object within array.push() [duplicate]为 array.push() 中的对象命名 [重复]
【发布时间】:2019-10-18 11:30:22
【问题描述】:

在我的部分代码中,我从 JSON 文件中提取数据并将其放置在基于对象的数组中。

我成功接收到数据,并且可以将每个数据放在数组中的单个对象上。唯一的问题是,我想为每个对象命名。

var playlist_data = {
  "Music1": {
    "soundcloud": "Soundcloud Music 1",
    "spotify": "Spotify Music 1"
  },
  "Music2": {
    "soundcloud": "Soundcloud Music 2",
    "spotify": "Spotify Music 2"
  },
  "Music3": {
    "soundcloud": "Soundcloud Music 3",
    "spotify": "Spotify Music 3"
  },
  "Music4": {
    "soundcloud": "Soundcloud Music 4",
    "spotify": "Spotify Music 4"
  }
};
var links = [];
$.each(playlist_data, function(index, element) {
  links.push({
    spotify: element.spotify,
    soundcloud: element.soundcloud,
  });
});

console.log(links);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

在上面的代码中看似复制了相同的 JSON 数据,但它是一个简化版本。

因此,我想将其称为 links.Music2links.Music2.spotify

【问题讨论】:

标签: javascript jquery arrays json object


【解决方案1】:

由于 Javascript 不支持命名索引,您必须将 links 设为对象才能通过 links.Music2.spotify 访问其属性。这很容易完成,但它只会为您提供与开始时完全相同的数据:

var playlist_data = {
  "Music1": {
    "soundcloud": "Soundcloud Music 1",
    "spotify": "Spotify Music 1"
  },
  "Music2": {
    "soundcloud": "Soundcloud Music 2",
    "spotify": "Spotify Music 2"
  },
  "Music3": {
    "soundcloud": "Soundcloud Music 3",
    "spotify": "Spotify Music 3"
  },
  "Music4": {
    "soundcloud": "Soundcloud Music 4",
    "spotify": "Spotify Music 4"
  }
};
var links = {};
$.each(playlist_data, function(index, element) {
  links[index]= {
    spotify: element.spotify,
    soundcloud: element.soundcloud,
  };
});

console.log(links);
console.log(links.Music2.spotify)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

【讨论】:

    【解决方案2】:

    您似乎想使用spotify 作为links 的键。

    JS中有两种数组:

    标准数组是:[ ]

    关联数组是:{ }!

    如您所见,您可以使用{ },它也可以作为javascript 中的对象作为数组使用。然后您可以使用spotify 作为密钥。 所以你的代码将如下所示:

    var links = {};
    $.each(playlist_data, function(index, element) {
      links[index] = {
        spotify: element.spotify,
        soundcloud: element.soundcloud,
      };
    });
    console.log(links_s.Music1.spotify) // Spotify Music 1
    

    【讨论】:

      【解决方案3】:

      您可以使用如下代码命名您的每个 obj

      var playlist_data = {
        "Music1": {
          "soundcloud": "Soundcloud Music 1",
          "spotify": "Spotify Music 1"
        },
        "Music2": {
          "soundcloud": "Soundcloud Music 2",
          "spotify": "Spotify Music 2"
        },
        "Music3": {
          "soundcloud": "Soundcloud Music 3",
          "spotify": "Spotify Music 3"
        },
        "Music4": {
          "soundcloud": "Soundcloud Music 4",
          "spotify": "Spotify Music 4"
        }
      };
      var links = [];
      $.each(playlist_data, function(index, element) {
        links.push({
          spotify: element.spotify,
          soundcloud: element.soundcloud,
        });
      });
      
      let myMusic = {};
      
      for(let x = 0 ; x < links.length ; x++)
      {
          let z = Number(x+1);
          myMusic["music"+z] = links[x];
      }
      
      console.log(myMusic.music1);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-14
        • 2016-09-04
        • 1970-01-01
        • 2014-06-24
        • 2019-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多