[英]I want to add to the Record Collection challenge from freeCodeCamp, but I'm stuck
Ok, so I went through the Record Collection tutorial and have a pretty solid understanding of the original challenge.好的,所以我浏览了 Record Collection 教程,并对原始挑战有了非常深入的了解。 However, I want my function to be able to add a new ID to the collection if it doesn't already exist.
但是,如果集合不存在,我希望我的函数能够将新 ID 添加到集合中。 I've tried so many variations, and I can't figure out how to do that (noob -_-).
我尝试了很多变体,但我不知道该怎么做(菜鸟-_-)。 I know this isn't necessary, but I think this will help with my overall understanding of objects and arrays anyway.
我知道这不是必需的,但我认为这将有助于我对对象和数组的整体理解。
Code below is my latest attempt.下面的代码是我最近的尝试。 The first if statement is my add-on.
第一个 if 语句是我的附加组件。 Should I be running an if statement with .hasOwnProperty first?
我应该先用 .hasOwnProperty 运行 if 语句吗? Idk.
同上。 Please explain in dummy terms.
请用虚词解释。 :)
:)
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");
If you are trying to make sure a record doesn't exist before adding it to a collection, the easiest way to do it is to use something like Array.find
.如果您试图在将记录添加到集合之前确保该记录不存在,最简单的方法是使用类似
Array.find
。
The below code only checks the id
field to see if it already exists.下面的代码只检查
id
字段以查看它是否已经存在。 In your scenario, you may want to check other properties, so your .find
will have some extra checks.在您的场景中,您可能想要检查其他属性,因此您的
.find
将进行一些额外的检查。
Example:例子:
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 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
Example with object
collection instead of array
of objects
使用
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
Another way to do it:另一种方法:
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.