【问题标题】:Store properties from object to array [closed]将属性从对象存储到数组[关闭]
【发布时间】:2021-03-11 23:05:35
【问题描述】:

我有这个 JSON 对象:

{    "Aatrox": {
      "version": "10.23.1",
      "id": "Aatrox",
      "key": "266",
      "name": "Aatrox",
      "title": "la Espada de los Oscuros",
      "image": {
         "full": "Aatrox.png",
         "sprite": "champion0.png",
         "group": "champion",
         "x": 0,
         "y": 0,
         "w": 48,
         "h": 48
      }    },    "Ahri": {
      "version": "10.23.1",
      "id": "Ahri",
      "key": "103",
      "name": "Ahri",
      "title": "La Mujer Zorro de nueve Colas",
      "image": {
         "full": "Ahri.png",
         "sprite": "champion0.png",
         "group": "champion",
         "x": 48,
         "y": 0,
         "w": 48,
         "h": 48
      }    },    "Akali": {
      "version": "10.23.1",
      "id": "Akali",
      "key": "84",
      "name": "Akali",
      "title": "la Asesina Sigilosa",
      "image": {
         "full": "Akali.png",
         "sprite": "champion0.png",
         "group": "champion",
         "x": 96,
         "y": 0,
         "w": 48,
         "h": 48
         }    },    "Alistar": {
      "version": "10.23.1",
      "id": "Alistar",
      "key": "12",
      "name": "Alistar",
      "title": "El Minotauro",
      "image": {
         "full": "Alistar.png",
         "sprite": "champion0.png",
         "group": "champion",
         "x": 144,
         "y": 0,
         "w": 48,
         "h": 48
      }    } }

我需要将每个对象的键“image.full”和“key”的值存储在一个新数组中。

我怎样才能用打字稿做到这一点?

【问题讨论】:

    标签: arrays angular object


    【解决方案1】:

    你可以这样实现:

    // Extract the "data"
    const { data } = { "type": "champion", "data": { "Aatrox": { "version": "10.23.1", "id": "Aatrox", "key": "266", "name": "Aatrox", "title": "la Espada de los Oscuros", "image": { "full": "Aatrox.png", "sprite": "champion0.png", "group": "champion", "x": 0, "y": 0, "w": 48, "h": 48 } }, "Ahri": { "version": "10.23.1", "id": "Ahri", "key": "103", "name": "Ahri", "title": "La Mujer Zorro de nueve Colas", "image": { "full": "Ahri.png", "sprite": "champion0.png", "group": "champion", "x": 48, "y": 0, "w": 48, "h": 48 } }, "Akali": { "version": "10.23.1", "id": "Akali", "key": "84", "name": "Akali", "title": "la Asesina Sigilosa", "image": { "full": "Akali.png", "sprite": "champion0.png", "group": "champion", "x": 96, "y": 0, "w": 48, "h": 48 } }, "Alistar": { "version": "10.23.1", "id": "Alistar", "key": "12", "name": "Alistar", "title": "El Minotauro", "image": { "full": "Alistar.png", "sprite": "champion0.png", "group": "champion", "x": 144, "y": 0, "w": 48, "h": 48 } } } };
    
    const images = Object.entries(data).map(([, { key, image }]) => ({ key, image: image.full }));
    
    console.log(images);

    【讨论】:

    • 谢谢。那么同一个 const 中的“key”呢?
    • @Robert,已经更新了上面的代码 sn-p。想知道这是否是预期的结果? :)
    • VS 代码出现此错误:类型“{}”上不存在属性“图像”。可能是因为我从这里得到了真正的 JSON:ddragon.leagueoflegends.com/cdn/10.23.1/data/es_ES/…@KShewengger
    • @罗伯特,我明白了。根据您发送的 JSON 链接,您要提取的列表位于数据对象属性下。因此,在您的变量中,您需要提取该 JSON 下的“数据”以继续该过程。已经更新了上面的sn-p。希望它有所帮助:)
    猜你喜欢
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 2020-01-10
    • 2018-06-04
    • 2023-02-09
    • 2021-11-04
    • 2019-09-02
    • 2017-07-17
    相关资源
    最近更新 更多