javascript - 在javascript中获取对象名称

标签 javascript jquery object local-storage

我想获取另一个对象中的对象名称。 这些是我的对象:

{
"events": {
"EED8A79F-B374-4AE6-BA6F-B7B98D9D7142": {
  "name": "Defeat the renegade charr.",
  "level": 42,
  "map_id": 20,
  "flags": [],
  "location": {
    "type": "sphere",
    "center": [ -9463.6, -40310.2, -785.799 ],
    "radius": 2500,
    "rotation": 0
  }
},
"3A2B85C5-DE73-4402-BD84-8F53AA394A52": {
  "name": "Bonus Event: Cull the Flame Legion",
  "level": 80,
  "map_id": 929,
  "flags": [ "group_event" ],
  "location": {
    "type": "cylinder",
    "center": [ -38.7202, -176.915, -892.494 ],
    "height": 2027.5,
    "radius": 10314.4,
    "rotation": 0
  }
},
"CEA84FBF-2368-467C-92EA-7FA60D527C7B": {
  "name": "Find a way to open the door and escape the armory.",
  "level": 8,
  "map_id": 19,
  "flags": [ "group_event" ],
  "location": {
    "type": "poly",
    "center": [ -45685.2, -13819.6, -1113 ],
    "z_range": [ -2389, 163 ],
    "points": [
      [ -49395.8, -15845.5 ],
      [ -42699.7, -15794.1 ],
      [ -43053, -14081.4 ],
      [ -43629.7, -11725.4 ],
      [ -49647.8, -11651.7 ]
    ]
  }
},
...
}
}

我使用这段代码来获取“events”对象中的所有对象并将它们保存在localStorage中:

$.each(data.events, function(index, value) {
    localStorage.setItem("Event" + event_count, data.events);
    event_count++;
});

但是现在我的localStorage是这样的: LocalStorage after using code above

如何获取对象名称而不是整个对象?我可以使用 data.events.level、data.events.map_id 获取级别、map_id 等,但如何获取对象名称,例如:“EED8A79F-B374-4AE6-BA6F-B7B98D9D7142”

提前致谢:)

最佳答案

localStorage 无法存储对象,只能存储字符串(它会在传递的任何非字符串的内容上调用 .toString,因此您会看到 [object对象])。

要存储对象,请使用 JSON.serialize() 写入它,并使用 JSON.parse() 读取它。

$.each 循环中,当前事件的 GUID 将位于 index 变量中。

关于javascript - 在javascript中获取对象名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20872587/

相关文章:

jquery - 无论如何改变复选框的背景颜色?

javascript - 如何在 dojo 中制作图像 slider ?

javascript - 将具有键的数组与对象 JavaScript 中的键进行比较

javascript - 设置 Vuetify 复选框的初始值

jQuery单页网站滚动/哈希问题

Javascript 定义对象,逗号大小写

java - 将新歌曲添加到专辑 java

javascript - 如何防止剪贴板回车变成换行

javascript - zone.js 和 rxjs 在 angular2 中的重要性

javascript - 根据值对 Javascript 对象进行排序