codecamp

JavaScript JSON集合操作

// Setup
var collection = {
2548: {
album: "Slippery When Wet",
artist: "Bon Jovi",
tracks: [ 
"Let It Rock", 
"You Give Love a Bad Name" 
]
},
2468: {
album: "1999",
artist: "Prince",
tracks: [ 
"1999", 
"Little Red Corvette" 
]
},
1245: {
artist: "Robert Palmer",
tracks: [ ]
},
5439: {
album: "ABBA Gold"
}
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));
// Only change code below this line
function update(id, prop, value) {
if(value !=='' && prop != 'tracks' ){
  collectionCopy[id][prop]=value;
}
  else if(value !==''&& prop == 'tracks'){
    collectionCopy[id][prop].push(value);
  }
  else if(value === ''){
    delete collectionCopy[id][prop];
  }
return collection;
}
// Alter values below to test your code
update(5439, "artist", "ABBA");
JavaScript 获取JSON数组值
JavaScript 使用for语句循环迭代
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定