Answer the question
In order to leave comments, you need to log in
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"
}
}
}]
}
$.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("Завершение выполнения");
});
$kursy = array(
"RU" => array(
"gramm" => array( "name", "img" ),
"prav" => array( "name", "img" ),
),
"EN" => array(
"gramm" => array( "name", "img" ),
),
)
Answer the question
In order to leave comments, you need to log in
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"
}]
}
}]
}
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 questionAsk a Question
731 491 924 answers to any question