Доступ к свойствам объекта JSON
Требуется написать цикл for in, чтобы отобразить в консоли имя каждого свойства первого элемента массива "singles". Цикл написал, но работает он некорректно... Нужна помощь с правкой кода
var albumInfoObj = {
"title": "Ray of Light",
"singer": "Madonna",
"year": 1998,
"songs": ["Swim", "Little Star", "Frozen", "Skin","The Power of Good-Bye"],
"recorded": {"studioName": "Larrabee North Studio", "months": 4.5},
"singles": [ {"name": "Frozen", "year": 1998},
{"name": "Nothing Really Matters", "year": 1999},
{"name": "Ray of Light", "year": 1998} ],
}
var propList = "";
for (var prop in albumInfoObj) {
propList += albumInfoObj.singles[0][prop] + "<br>";
}
console.log(propList);
|
crabkilla,
var albumInfoObj = {
"title": "Ray of Light",
"singer": "Madonna",
"year": 1998,
"songs": ["Swim", "Little Star", "Frozen", "Skin", "The Power of Good-Bye"],
"recorded": {
"studioName": "Larrabee North Studio",
"months": 4.5
},
"singles": [{
"name": "Frozen",
"year": 1998
}, {
"name": "Nothing Really Matters",
"year": 1999
}, {
"name": "Ray of Light",
"year": 1998
}],
}
var propList = [];
for (var prop in albumInfoObj.singles[0]) {
propList.push(prop);
}
console.log(propList.join("\n"));
Цитата:
var albumInfoObj = {
"title": "Ray of Light",
"singer": "Madonna",
"year": 1998,
"songs": ["Swim", "Little Star", "Frozen", "Skin", "The Power of Good-Bye"],
"recorded": {
"studioName": "Larrabee North Studio",
"months": 4.5
},
"singles": [{
"name": "Frozen",
"year": 1998
}, {
"name": "Nothing Really Matters",
"year": 1999
}, {
"name": "Ray of Light",
"year": 1998
}],
}
console.log(Object.keys(albumInfoObj.singles[0]).join("\n"));
|
crabkilla,
const albumInfoObj = {
"title": "Ray of Light",
"singer": "Madonna",
"year": 1998,
"songs": ["Swim", "Little Star", "Frozen", "Skin","The Power of Good-Bye"],
"recorded": {"studioName": "Larrabee North Studio", "months": 4.5},
"singles": [ {"name": "Frozen", "year": 1998},
{"name": "Nothing Really Matters", "year": 1999},
{"name": "Ray of Light", "year": 1998} ],
}
let propList = "";
for (const prop of albumInfoObj.singles) {
propList += prop.name + "\n";
}
console.log(propList);
|
| Часовой пояс GMT +3, время: 15:08. |