Okay, I have a record collection object:
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"
}
};
The code is updated, with the following function below:
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 {
delete collection[id][prop];
}
return collection;
}
I'm looking for some clarification on how the above code snippet functions. I read it as something like:
if (prop === "tracks" && value !== "") {
if(collection[id][prop]) {
collection[id][prop].push(value);
}
If 'prop' is equal to tracks and the value is not blank, proceed into the updating portion of the function. The next part, which I'm not certain of, seems to say something like 'if collection's id and prop have a value, push said id and prop into the collection.
The next part I'm definitely a little lost on:
else {
collection[id][prop]=[value];
}
Starting with the above, I'm unclear if the purpose of this else statement is to just verify whether the information is already contained within the array.
else if (value !== "") {
collection[id][prop] = value;
} else {
delete collection[id][prop];
}
Finally, the above seems to test whether just the value is blank and, if it's not, it inserts said value into the array. If all conditions fail, the id and prop aren't added to the array. I don't feel I'm way off on this, but if somebody could help clarify some of the confusion, it would be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire