[英]I want to add to the Record Collection challenge from freeCodeCamp, but I'm stuck
好的,所以我浏览了 Record Collection 教程,并对原始挑战有了非常深入的了解。 但是,如果集合不存在,我希望我的函数能够将新 ID 添加到集合中。 我尝试了很多变体,但我不知道该怎么做(菜鸟-_-)。 我知道这不是必需的,但我认为这将有助于我对对象和数组的整体理解。
下面的代码是我最近的尝试。 第一个 if 语句是我的附加组件。 我应该先用 .hasOwnProperty 运行 if 语句吗? 同上。 请用虚词解释。 :)
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"
}
};
function updateRecords(id, prop, value) {
// If the id is not blank and the prop is not blank,
if (id !== "" && prop !== "") {
// then create the new id name and push the property onto it.
collection.id = [];
collection[id].push(prop);
}
//If the property is equal to "tracks" and the tracks value isn't empty,
else if (value !== "" && prop === "tracks") {
//update or set the value for the property.
collection[id][prop].tracks;
//If the specificied id doesn't have the property tracks,
} else if (!collection[id].hasOwnProperty("tracks")) {
//then add the property tracks and push in the track's name value
collection[id].tracks = [];
collection[id].tracks.push(value);
//Otherwise delete the id entirely.
} else {
delete collection[id][prop];
}
return collection;
}
updateRecords(2005, "tracks", "check on it");
如果您试图在将记录添加到集合之前确保该记录不存在,最简单的方法是使用类似Array.find
。
下面的代码只检查id
字段以查看它是否已经存在。 在您的场景中,您可能想要检查其他属性,因此您的.find
将进行一些额外的检查。
例子:
const records = [ { id: 1, name: 'Test1' }, { id: 2, name: 'Test2' }, { id: 3, name: 'Test3' }, { id: 4, name: 'Test4' }, ]; const addRecord = (record) => { if (record) { const existingRecord = records.find(r => r.id === record.id); if (existingRecord) { return `Record ${record.id} already exists`; } records.push(record); return records; } return 'Record cannot be falsy'; } console.log(addRecord({ id: 5, name: 'Test5' })); // Adds record console.log(addRecord({ id: 1, name: 'Test123123123 doesnt matter' })); // Record already exists
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
使用object
集合而不是objects
array
的示例
let records = { 1: { name: 'Test1' }, 2: { name: 'Test2' }, 3: { name: 'Test3' }, 4: { name: 'Test4' } }; const findRecord = (recordId) => { for(const key in records) { if (+key === recordId) return records[key]; } return null; }; const addRecord = (id, name) => { if (id && name) { const recordExists = findRecord(id); if (recordExists) return `Record ${id} already exists`; records[id] = { name }; return records; } return 'Record cannot be falsy'; }; console.log(addRecord(5, 'Test5')); // Adds record console.log(addRecord(1, 'blag asdfsdafafds')); // Already exists
另一种方法:
let records = {
1: { name: 'Test1' },
2: { name: 'Test2' },
3: { name: 'Test3' },
4: { name: 'Test4' }
}
const addRecord = (id, name) => {
if (!id || !name) {
return 'Id and name must be defined'
}
const exists = Object.keys(records).some(recordId => +recordId === id)
if (exists) {
return `${id} already exists`
}
records[id] = { name }
return records
}
console.log(addRecord(5, 'Test5')) // Adds record
console.log(addRecord(1, 'blag asdfsdafafds')) // Already exists
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.