A
A
Alexey Medvedev2019-01-22 10:36:46
JavaScript
Alexey Medvedev, 2019-01-22 10:36:46

How to create multidimensional array from JSON request?

Good afternoon, tell me how to create a multidimensional array from a JSON response?
I get the following response from the server:

{
  "RU":[{
    "name":"Русский",
    "img":"/flag/RU.png",
    "courses":{
      "gramm":[{
        "name":"Грамматика",
        "img":"/icon1.png"
      }],
      "prav":[{
        "name":"Правописание",
        "img":"/icon2.png"
      }]
    }
  }],
  "EN":[{
    "name":"Английский",
    "img":"/flag/EN.png",
    "coursess":{
      "gramm":{
        "name":"Грамматика",
        "img":"/icon1.png"
        }
      }
  }]
}


and there is such a handler:

$.getJSON(countryUrl)
    .done(function(data){
      var strany = [];
      var kursy = [];
      $.each(data, function(key, info){
        $.each(info, function(index, value){
          strany.push([key, value['name'], value['img']]);
                                       // тут нормальный массив Array('RU', 'Русский', '/flag/RU.png')
          $.each(value['courses'], function(k, v){
            $.each(v, function(k1, v1){
              //console.log('Название = ' + v1['name'] + '; icon = '+ v1['img']);
              // тут я пытался собрать массив, но js выдает ошибку
            });
          });
        });
      });
         	console.log(strany);
      console.log(kursy);
    })
    .fail(function(){
      //alert("Ошибка выполнения");
    })
    .then(function(){
      //alert("Завершение выполнения");
    });


The COURSES array should be of the following form (for example, in PHP it would look like this):
$kursy = array(
     "RU" => array(
                     "gramm" => array( "name", "img" ),
                     "prav" => array( "name", "img" ),
                 ),
     "EN" => array(
                     "gramm" => array( "name", "img" ),
                 ),
)


Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Petr Muhurov, 2019-01-22
@medvedhack

const data = {
  "RU":[{
    "name":"Русский",
    "img":"/flag/RU.png",
    "courses":{
      "gramm":[{
        "name":"Грамматика",
        "img":"/icon1.png"
      }],
      "prav":[{
        "name":"Правописание",
        "img":"/icon2.png"
      }]
    }
  }],
  "EN":[{
    "name":"Английский",
    "img":"/flag/EN.png",
    "courses":{
      "gramm":[{
        "name":"Грамматика",
        "img":"/icon3.png"
        }]
      }
  }]
}

I would like to see valid data next time, and not sit and edit it myself;)
const countries = [], courses = [];

$.each(data, (lng, [{ name, img, courses: crs }]) => {
  countries.push([lng, name, img]);
  
  $.each(crs, (crsName, [{ name, img }]) => {
    courses.push([lng, name, img])
  })
});

countries // 
[
  ["RU", "Русский", "/flag/RU.png"],
  ["EN", "Английский", "/flag/EN.png"]
]
courses //
[
  ["RU", "Грамматика", "/icon1.png"],
  ["RU", "Правописание", "/icon2.png"],
  ["EN", "Грамматика", "/icon3.png"]
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question