2013-12-26 2 views
-2

У меня вопрос о реорганизации (есть ли подходящий термин для этого?) Массив в jQuery.Повторно организовать массив с помощью JQuery

Ниже приведен образец рассматриваемого массива. В основном это показывает список видеоигр, каждая игра имеет еще один вложенный массив для платформ (консолей, в которых доступна игра).

Я хочу изменить массив, чтобы удалить вложенный массив и дублировать верхний уровень. Я пытаюсь использовать $ .map jQuery безрезультатно.

Я хочу, чтобы получить от этого

game1 
    xbox 
    ps3 
    PC 
game2 
    xbox 
    wii 

к этому

game1, xbox 
game1, ps3 
game1, PC 
game2, xbox 
game2, wii 

ниже является фактическим массиву

myCallback({ 
    error: "OK", 
    limit: 5, 
    offset: 0, 
    number_of_page_results: 5, 
    number_of_total_results: 26, 
    status_code: 1, 
    results: [{ 
     image: { 
      icon_url: "http://static.giantbomb.com/uploads/square_avatar/8/87790/1784831-box_bf2.png", 
      medium_url: "http://static.giantbomb.com/uploads/scale_medium/8/87790/1784831-box_bf2.png", 
      screen_url: "http://static.giantbomb.com/uploads/screen_medium/8/87790/1784831-box_bf2.png", 
      small_url: "http://static.giantbomb.com/uploads/scale_small/8/87790/1784831-box_bf2.png", 
      super_url: "http://static.giantbomb.com/uploads/scale_large/8/87790/1784831-box_bf2.png", 
      thumb_url: "http://static.giantbomb.com/uploads/scale_avatar/8/87790/1784831-box_bf2.png", 
      tiny_url: "http://static.giantbomb.com/uploads/square_mini/8/87790/1784831-box_bf2.png" 
     }, 
     name: "Battlefield 2", 
     platforms: [{ 
      api_detail_url: "http://www.giantbomb.com/api/platform/3045-94/", 
      id: 94, 
      name: "PC", 
      site_detail_url: "http://www.giantbomb.com/pc/3045-94/", 
      abbreviation: "PC" 
     }], 
     resource_type: "game" 
    }, { 
     image: { 
      icon_url: "http://static.giantbomb.com/uploads/square_avatar/8/87790/2409898-box_bfbc2.png", 
      medium_url: "http://static.giantbomb.com/uploads/scale_medium/8/87790/2409898-box_bfbc2.png", 
      screen_url: "http://static.giantbomb.com/uploads/screen_medium/8/87790/2409898-box_bfbc2.png", 
      small_url: "http://static.giantbomb.com/uploads/scale_small/8/87790/2409898-box_bfbc2.png", 
      super_url: "http://static.giantbomb.com/uploads/scale_large/8/87790/2409898-box_bfbc2.png", 
      thumb_url: "http://static.giantbomb.com/uploads/scale_avatar/8/87790/2409898-box_bfbc2.png", 
      tiny_url: "http://static.giantbomb.com/uploads/square_mini/8/87790/2409898-box_bfbc2.png" 
     }, 
     name: "Battlefield: Bad Company 2", 
     platforms: [{ 
      api_detail_url: "http://www.giantbomb.com/api/platform/3045-20/", 
      id: 20, 
      name: "Xbox 360", 
      site_detail_url: "http://www.giantbomb.com/xbox-360/3045-20/", 
      abbreviation: "X360" 
     }, { 
      api_detail_url: "http://www.giantbomb.com/api/platform/3045-35/", 
      id: 35, 
      name: "PlayStation 3", 
      site_detail_url: "http://www.giantbomb.com/playstation-3/3045-35/", 
      abbreviation: "PS3" 
     }, { 
      api_detail_url: "http://www.giantbomb.com/api/platform/3045-94/", 
      id: 94, 
      name: "PC", 
      site_detail_url: "http://www.giantbomb.com/pc/3045-94/", 
      abbreviation: "PC" 
     }, { 
      api_detail_url: "http://www.giantbomb.com/api/platform/3045-96/", 
      id: 96, 
      name: "iPhone", 
      site_detail_url: "http://www.giantbomb.com/iphone/3045-96/", 
      abbreviation: "IPHN" 
     }, { 
      api_detail_url: "http://www.giantbomb.com/api/platform/3045-121/", 
      id: 121, 
      name: "iPad", 
      site_detail_url: "http://www.giantbomb.com/ipad/3045-121/", 
      abbreviation: "IPAD" 
     }], 
     resource_type: "game" 
    }, { 
     image: { 
      icon_url: "http://static.giantbomb.com/uploads/square_avatar/6/63119/1711171-battlefield_play4free.jpg", 
      medium_url: "http://static.giantbomb.com/uploads/scale_medium/6/63119/1711171-battlefield_play4free.jpg", 
      screen_url: "http://static.giantbomb.com/uploads/screen_medium/6/63119/1711171-battlefield_play4free.jpg", 
      small_url: "http://static.giantbomb.com/uploads/scale_small/6/63119/1711171-battlefield_play4free.jpg", 
      super_url: "http://static.giantbomb.com/uploads/scale_large/6/63119/1711171-battlefield_play4free.jpg", 
      thumb_url: "http://static.giantbomb.com/uploads/scale_avatar/6/63119/1711171-battlefield_play4free.jpg", 
      tiny_url: "http://static.giantbomb.com/uploads/square_mini/6/63119/1711171-battlefield_play4free.jpg" 
     }, 
     name: "Battlefield Play4Free", 
     platforms: [{ 
      api_detail_url: "http://www.giantbomb.com/api/platform/3045-94/", 
      id: 94, 
      name: "PC", 
      site_detail_url: "http://www.giantbomb.com/pc/3045-94/", 
      abbreviation: "PC" 
     }], 
     resource_type: "game" 
    }, { 
     image: { 
      icon_url: "http://static.giantbomb.com/uploads/square_avatar/3/34737/1032047-bf1942_front.jpg", 
      medium_url: "http://static.giantbomb.com/uploads/scale_medium/3/34737/1032047-bf1942_front.jpg", 
      screen_url: "http://static.giantbomb.com/uploads/screen_medium/3/34737/1032047-bf1942_front.jpg", 
      small_url: "http://static.giantbomb.com/uploads/scale_small/3/34737/1032047-bf1942_front.jpg", 
      super_url: "http://static.giantbomb.com/uploads/scale_large/3/34737/1032047-bf1942_front.jpg", 
      thumb_url: "http://static.giantbomb.com/uploads/scale_avatar/3/34737/1032047-bf1942_front.jpg", 
      tiny_url: "http://static.giantbomb.com/uploads/square_mini/3/34737/1032047-bf1942_front.jpg" 
     }, 
     name: "Battlefield 1942", 
     platforms: [{ 
      api_detail_url: "http://www.giantbomb.com/api/platform/3045-17/", 
      id: 17, 
      name: "Mac", 
      site_detail_url: "http://www.giantbomb.com/mac/3045-17/", 
      abbreviation: "MAC" 
     }, { 
      api_detail_url: "http://www.giantbomb.com/api/platform/3045-94/", 
      id: 94, 
      name: "PC", 
      site_detail_url: "http://www.giantbomb.com/pc/3045-94/", 
      abbreviation: "PC" 
     }], 
     resource_type: "game" 
    }, { 
     image: { 
      icon_url: "http://static.giantbomb.com/uploads/square_avatar/8/87790/1897518-box_bf2142.png", 
      medium_url: "http://static.giantbomb.com/uploads/scale_medium/8/87790/1897518-box_bf2142.png", 
      screen_url: "http://static.giantbomb.com/uploads/screen_medium/8/87790/1897518-box_bf2142.png", 
      small_url: "http://static.giantbomb.com/uploads/scale_small/8/87790/1897518-box_bf2142.png", 
      super_url: "http://static.giantbomb.com/uploads/scale_large/8/87790/1897518-box_bf2142.png", 
      thumb_url: "http://static.giantbomb.com/uploads/scale_avatar/8/87790/1897518-box_bf2142.png", 
      tiny_url: "http://static.giantbomb.com/uploads/square_mini/8/87790/1897518-box_bf2142.png" 
     }, 
     name: "Battlefield 2142", 
     platforms: [{ 
      api_detail_url: "http://www.giantbomb.com/api/platform/3045-17/", 
      id: 17, 
      name: "Mac", 
      site_detail_url: "http://www.giantbomb.com/mac/3045-17/", 
      abbreviation: "MAC" 
     }, { 
      api_detail_url: "http://www.giantbomb.com/api/platform/3045-94/", 
      id: 94, 
      name: "PC", 
      site_detail_url: "http://www.giantbomb.com/pc/3045-94/", 
      abbreviation: "PC" 
     }], 
     resource_type: "game" 
    }], 
    version: "1.0" 
}) 
+0

Не относится к JQuery на всех – zerkms

ответ

0
var rightArray = [];  
for (game in gameArray) { 
    for (con in gameArray[game]) { 
    rightArray.push(game + ', ' + gameArray[game][con]); 
    } 
} 
Смежные вопросы