代码之家  ›  专栏  ›  技术社区  ›  Rowan Kehn

如何从JSON数据的特定部分创建数组?

  •  1
  • Rowan Kehn  · 技术社区  · 1 年前

    在调用API之后,我得到了一些JSON格式的数据。在这种格式中,数组的每个键内都有一个包含子部分的数组(很抱歉,我不知道术语)。

    {
      "id": "<my id>",
      "bots": [],
      "avatar": "https://static-cdn.jtvnw.net/jtv_user_pictures/1c1c09ef-3937-4408-8f31-04ec6bec2238-profile_image-300x300.png",
      "channelEmotes": [],
      "sharedEmotes": [
        {
          "id": "<emote id>",
          "code": "<emote name>",
          "imageType": "<filetype>",
          "animated": true,
          "user": {
            "id": "<emote creator id>",
            "name": "<emote creator username>",
            "displayName": "<emote creator display name>",
            "providerId": "<not sure what this field is actually>"
          }
        },
        {
          "id": "<emote id>",
          "code": "<emote name>",
          "imageType": "<filetype>",
          "animated": true,
          "user": {
            "id": "<emote creator id>",
            "name": "<emote creator username>",
            "displayName": "<emote creator display name>",
            "providerId": "<not sure what this field is actually>"
          }
        }
      ]
    }
    

    具体来说,我想为所有emote名称、emote id和文件类型创建单独的数组,例如(我计划有比这两个更多的数组)

    var emotecodes = [code0, code1, ...];
    var emoteids = [id0, id1, ...];
    var emotefiletypes = [imageType0, imageType1, ...];
    

    我尝试了在互联网上找到的各种其他东西,但都没有成功。

    4 回复  |  直到 1 年前
        1
  •  2
  •   brk    1 年前

    您可以在上使用reduce函数 sharedEmotes json的属性

    const jsonData = {
      "id": "<my id>",
      "bots": [],
      "avatar": "https://static-cdn.jtvnw.net/jtv_user_pictures/1c1c09ef-3937-4408-8f31-04ec6bec2238-profile_image-300x300.png",
      "channelEmotes": [],
      "sharedEmotes": [{
          "id": "<emote id>",
          "code": "<emote name>",
          "imageType": "<filetype>",
          "animated": true,
          "user": {
            "id": "<emote creator id>",
            "name": "<emote creator username>",
            "displayName": "<emote creator display name>",
            "providerId": "<not sure what this field is actually>"
          }
        },
        {
          "id": "<emote id>",
          "code": "<emote name>",
          "imageType": "<filetype>",
          "animated": true,
          "user": {
            "id": "<emote creator id>",
            "name": "<emote creator username>",
            "displayName": "<emote creator display name>",
            "providerId": "<not sure what this field is actually>"
          }
        }
      ]
    };
    
    const formattedData = jsonData.sharedEmotes.reduce((acc, curr) => {
    
      acc.emotecodes.push(curr.code);
      acc.emoteids.push(curr.id);
      acc.emotefiletypes.push(curr.imageType);
    
    
      return acc;
    }, {
      emotecodes: [],
      emoteids: [],
      emotefiletypes: []
    });
    
    
    console.log(formattedData.emotecodes, formattedData.emoteids, formattedData.emotefiletypes)
        2
  •  2
  •   dharma-ká¹£etre    1 年前

    要从JSON数据中为emote名称、emote id和文件类型创建单独的数组,可以使用 map JavaScript中的函数。这个 地图 函数将允许您提取JSON数据的特定部分,并基于这些部分创建新的数组。

    const emotecodes = data.sharedEmotes.map(emote => emote.code);
    const emoteids = data.sharedEmotes.map(emote => emote.id);
    const emotefiletypes = data.sharedEmotes.map(emote => emote.imageType)
    
        3
  •  1
  •   Giri    1 年前

    Array.prototype.map() 方法可以完成这项工作。还可以考虑使用 const let 声明变量而不是 var ( Here 这就是为什么)。

    const apiJSONData = {
        id: '<my id>',
        bots: [],
        avatar:
          'https://static-cdn.jtvnw.net/jtv_user_pictures/1c1c09ef-3937-4408-8f31-04ec6bec2238-profile_image-300x300.png',
        channelEmotes: [],
        sharedEmotes: [
          {
            id: '<emote id>',
            code: '<emote name>',
            imageType: '<filetype>',
            animated: true,
            user: {
              id: '<emote creator id>',
              name: '<emote creator username>',
              displayName: '<emote creator display name>',
              providerId: '<not sure what this field is actually>',
            },
          },
          {
            id: '<emote id>',
            code: '<emote name>',
            imageType: '<filetype>',
            animated: true,
            user: {
              id: '<emote creator id>',
              name: '<emote creator username>',
              displayName: '<emote creator display name>',
              providerId: '<not sure what this field is actually>',
            },
          },
        ],
      };
    
      const emoteCodes = apiJSONData.sharedEmotes.map((emoteObj) => emoteObj.code);
      const emoteIds = apiJSONData.sharedEmotes.map((emoteObj) => emoteObj.id);
      const emoteFileTypes = apiJSONData.sharedEmotes.map(
        (emoteObj) => emoteObj.imageType
      );
    
      console.log(emoteCodes);
      console.log(emoteIds);
      console.log(emoteFileTypes);
        4
  •  -2
  •   thecodingsage    1 年前

    这是一种老式的获取数据的方法。(O(N)):

    const getData = (json, type) => {
      let result = []
      for (let i of json['sharedEmotes']) {
        result.push(i[type])
      }
      return result
    }
    

    它只是迭代sharedMotes数组,并将类型推送到一个新数组中。

    以下是完整的代码:

    const json = {
      "id": "<my id>",
      "bots": [],
      "avatar": "https://static-cdn.jtvnw.net/jtv_user_pictures/1c1c09ef-3937-4408-8f31-04ec6bec2238-profile_image-300x300.png",
      "channelEmotes": [],
      "sharedEmotes": [{
          "id": "<emote id>",
          "code": "<emote name>",
          "imageType": "<filetype>",
          "animated": true,
          "user": {
            "id": "<emote creator id>",
            "name": "<emote creator username>",
            "displayName": "<emote creator display name>",
            "providerId": "<not sure what this field is actually>"
          }
        },
        {
          "id": "<emote id>",
          "code": "<emote name>",
          "imageType": "<filetype>",
          "animated": true,
          "user": {
            "id": "<emote creator id>",
            "name": "<emote creator username>",
            "displayName": "<emote creator display name>",
            "providerId": "<not sure what this field is actually>"
          }
        }
      ]
    }
    
    const getData = (json, type) => {
      let result = []
      for (let i of json['sharedEmotes']) {
        result.push(i[type])
      }
      return result
    }
    
    let emotecodes = getData(json, 'code');
    let emoteids = getData(json, 'id');
    let emotefiletypes = getData(json, 'imageType');
    
    console.log(emotecodes)
    console.log(emoteids)
    console.log(emotefiletypes)