这是提示:有一个JSON对象,代表着你的专辑集。每一张专辑由一个唯一的id标识,并具有多种属性。但并非所有的专辑都有完整的信息。写一个函数,它有个三个参数,id、prop、value。如果 value !='' 而且prop != 'tracks',collectionCopy[id][prop]=value;。如果 value !='' 而且prop == 'tracks',collectionCopy[id][prop].push(value);。如果 value == '' ,delete collectionCopy[id][prop];。这是要求:update(5439, "artist", "ABBA") 之后, artist 应该为"ABBA"update(2548, "artist", "") 之后, artist 应该不存在update(1245, "tracks", "Addicted to Love") 之后,tracks 的最后一个元素应该是"Addicted to Love"update(2548, "tracks", "") 之后, tracks 应该不存在我代码这样不知道那里有问题: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" }};var collectionCopy = JSON.parse(JSON.stringify(collection));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 colectionCopy[id][prop]; } return collection;}update(5439, "artist", "ABBA");
6 回答
离歌笑_0002
TA贡献1条经验 获得超0个赞
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;
}
单引号或者双引号内不留空格
qq_怠惰_04293838
TA贡献1条经验 获得超0个赞
不用写else, 直接if, 应该是最先判断value是不是空, 这个是执行顺序的问题, 然后应该是return collectionCopy,而不是collection
添加回答
举报
0/150
提交
取消