gpt4 book ai didi

javascript - 自由代码营 : Record Collection return "Cannot read property ' push' of undefined"

转载 作者:行者123 更新时间:2023-12-01 00:52:59 26 4
gpt4 key购买 nike

挑战是来自 Freecodecamp 的“Basic JavaScript: Record Collection”。挑战是更新 JSON 格式的记录集合。

这是我编写的代码:

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 (prop !== "tracks" && value !== "") {
collection[id][prop] = value;
} else if (prop === "tracks" && value !== "") {
collection[id][prop].push(value);
} else if (value == "") {
delete collection[id][prop];
} else if (prop === "tracks") {
collection[id][prop] = [value];
}


return collection;
}

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

它满足除一项要求之外的所有要求:

After updateRecords(5439, "tracks", "Take a Chance on Me"), tracks should have "Take a Chance on Me" as the last element.

当我检查控制台时,我得到了这个声明

Cannot read property 'push' of undefined

看起来代码认为push(应该是一个函数)实际上是一个对象属性。

如何修复它?

如果您想详细了解此挑战,请点击以下链接: https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-javascript/record-collection/

最佳答案

目前,您的代码永远不会达到 else if (prop === "tracks") 条件。如果 prop === "tracks" ,它将转到 prop === "tracks"&& value !== "" 条件并尝试推送 valuetracks 数组。如果idtracks数组不存在(例如5439),它将抛出错误。因此,您可以按如下方式更改条件

检查属性是否为tracks。在此 if 条件内检查 collection[id][prop] 是否具有值。如果是,则将其推送到数组中。否则,添加一个新数组,如下所示 [value]

function updateRecords(id, prop, value) {
if (prop === "tracks" && value !== "") {

// check if tracks exists
if (collection[id][prop])
collection[id][prop].push(value)
else
collection[id][prop] = [value]; // create a new array

} else if (value != "") {
collection[id][prop] = value;
} else if (value == "") {
delete collection[id][prop];
}

return collection;
}

这是一个工作片段:

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 (prop === "tracks" && value !== "") {

if(collection[id][prop])
collection[id][prop].push(value)
else
collection[id][prop] = [value]

} else if (value != "") {
collection[id][prop] = value;
} else if (value == "") {
delete collection[id][prop];
}

return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
updateRecords(5439, "tracks", "Take a Chance on Me");

console.log(collection[5439])

关于javascript - 自由代码营 : Record Collection return "Cannot read property ' push' of undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56823311/

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