gpt4 book ai didi

javascript - 为什么我的 Javascript 代码没有向对象添加新属性?

转载 作者:行者123 更新时间:2023-12-01 00:33:02 28 4
gpt4 key购买 nike

我正在学习 JavaScript 的 freeCodeCamp 教程,并且一直在完成记录集合作业。我正在尝试以不同于所呈现的方式编写它,但是尽管浏览了过去的教程并在线搜索,我无法理解为什么我的代码没有将属性“艺术家”附加到 5439 对象。

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 updateRecords(id, prop, value) {
for (var i in collection){
if (i == id){
if (value == ""){
delete collection.i.prop;
}
else if (prop == "tracks" && value !== ""){
if (collections.hasOwnProperty("tracks")){
i.tracks.push(value);
}
else {
i["tracks"] = value;
}
}
else {
i.prop = value;
}
}
}
return collection;
}

// Alter values below to test your code
console.log(updateRecords(5439, "artist", "ABBA"));

我浏览了在线教程,看起来我正确地包含了它。我也尝试过括号表示法,但没有任何效果,但是当我调试代码并打印结果时,没有添加任何内容。我根本就没有访问该对象吗?

最佳答案

您需要使用bracket notation当使用变量动态访问对象集合中的属性时。当您使用 for...in 循环时,您的 i 变量引用对象中的键,因此它不会具有 .tracks 等属性 等...(这意味着诸如 i.tracks.push(value) 之类的代码将不起作用),您需要获取对存储在键 i< 处的对象的引用 使用 collection[i]:

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 updateRecords(id, prop, value) {
for (var i in collection){
if (i == id){
if (value == ""){
delete collection[i][prop];
}
else if (prop == "tracks" && value !== ""){
if (collections.hasOwnProperty("tracks")){
collection[i].tracks.push(value);
}
else {
collection[i].tracks = value;
}
}
else {
collection[i][prop] = value;
}
}
}
return collection;
}

// Alter values below to test your code
console.log(updateRecords(5439, "artist", "ABBA"));

但是,由于您当前所做的只是更新对象,因此在提供 id 时不需要 for...in 循环>,作为更新的关键:

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 updateRecords(id, prop, value) {
if (id in collection) {
var to_update = collection[id];
if (value == "") {
delete to_update[prop];
} else if (prop == "tracks") { // only get to this point if `value !== ""`, so there is no need to check `&& value !== ""`
if ('tracks' in to_update)
to_update.tracks.push(value);
else
to_update.tracks = value;
} else {
to_update[prop] = value;
}
}
return collection;
}

// Alter values below to test your code
console.log(updateRecords(5439, "artist", "ABBA"));

关于javascript - 为什么我的 Javascript 代码没有向对象添加新属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58350531/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com