Forgive me for so much text.
Based on your description and corrections, your code looks like this, right?
// Only change code below this line
function updateRecords(records, id, prop, value) {
if(value == ""){
delete records[id][prop];
}
else if (prop !== "tracks" && value !== "") {
records[id][prop] = value;
}
else if(prop === "tracks" && value !== ""){
if(records.hasOwnProperty(prop)){
records[id][prop].push(value);
}
else{
var array = [];
array.push(value);
records[id] = "tracks";
records[id][tracks] = array;
}
}
return records;
}
Well, let’s focus on the second else if:
First conditional
if(records.hasOwnProperty(prop)){
records[id][prop].push(value); -> This will search for the existence of a property within the main part (root) of the 'recordCollection' array. (This search does not extend into nested objects, meaning it focuses only on checking if the property exists within the root of the array.)
}
Second conditional
else{
var array = []; -> creates an array
array.push(value); -> adds the value
records[id] = "tracks"; -> replaces the key value id for 'tracks'
records[id][tracks] = array; -> creates and sets the key 'tracks'
}}
So, for the first conditional, you aren’t searching for a specific id, let me add an example:
Let’s say I have an array of objects called ‘musicCollection’:
const musicCollection = {
1: {
musicName: 'name3',
artist: 'Artist 3',
tracks: ['Track 5', 'Track 6']
},
2: {
musicName: 'name3',
artist: 'Artist 3',
tracks: ['Track 5', 'Track 6']
},
3: {
musicName: 'name3',
artist: 'Artist 3',
tracks: ['Track 5', 'Track 6'],
limitedEdition: true
}
}
and I want to verify if the property 'limitedEdition' exists. So we gone make use of 'hasOwnProperty('limitedEdition')'. Although we may have two scenarios:
First scenarios:
We don’t specify the [id], so 'hasOwnProperty' will only search the properties located in the root (1,2 and 3):
const musicCollection = {
1: {
...properties
},
2: {
...properties
},
3: {
...properties
}
}
Second scenarios:
We Specify the id, so 'hasOwnProperty' will iterate over the properties of the specified id:
const musicCollection = {
1: {
...properties
},
2: {
...properties
},
3: {
musicName: 'name3',
albumTitle: 'Album 3',
artist: 'Artist 3',
tracks: ['Track 5', 'Track 6']
limitedEdition: true -> exists
}
}
musicCollection[3].hasOwnProperty('limitedEdition') -> true:
Try to understand and use it
Last, your 'else' condition:
else{
var array = []; -> creates an array
array.push(value); -> adds the value
records[id] = "tracks"; -> replaces the key value id for 'tracks'
records[id][tracks] = array; -> creates and sets the key 'tracks'
}}
The problem here, is that records[id] = "tracks"; replaces the key [id] for 'tracks':
1245: {
artist: 'Robert Palmer',
tracks: []
},
"tracks": { -> It may end like this, replacing the key [id]
albumTitle: 'ABBA Gold'
}
So just remove the line records[id] = "tracks";, since 'records[id][tracks] = array;', is already doing the same (creating the key and setting the value, a one-line)
Last, you aren’t accessing correctly the property 'records[id][tracks] = array;'. I will provide a link that explains how to access dynamic properties
Try to understand it, make the adjustments and it should work, if not ask again.
Cheers