sp2012
June 26, 2019, 7:51am
1
Hello,
I have this code:
// Setup
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[prop].push(value);
}
if(prop === "tracks" && collection.hasOwnProperty("tracks") === false) {
collection.tracks = [];
collection.tracks.push(value);
}
if(prop === "tracks" && value !== "") {
collection.tracks.push(value);
}
if(value === ""){
delete collection[prop];
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
I am getting error: Cannot read property âpushâ of undefined
Any ideas?
Here on line 35-
Youâre trying to access the property âartistâ of the object collection
. That property doesnât exist. You need to first make use of the id
argument youâre passing in to the updateRecords
function.
sp2012
June 26, 2019, 11:00am
3
Thanks. How do I make use of the id argument?
You can chain together property accessors to go through the different levels in nested objects. In this case-
collection[id][prop]
will get you the array you are looking to push to.
sp2012
June 26, 2019, 3:14pm
5
Thanks.
OK, so now I have this code:
// Setup
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].push(value);
}
if(prop === "tracks" && collection.hasOwnProperty("tracks") === false) {
collection.id.tracks = [];
collection.id.tracks.push(value);
}
if(prop === "tracks" && value !== "") {
collection.id.tracks.push(value);
}
if(value === ""){
delete collection[id][prop];
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
but I still get error: Cannot read property âpushâ of undefined
Ah I see.
push()
is used to add a value to an existing array. Thatâs not what youâre trying to do in this instance.
With updateRecords(5439, "artist", "ABBA")
, youâre trying to add a new key value pair to the object â5439â.
Instead of using push()
, you need to use an assignment operator =
.
sp2012
June 26, 2019, 4:19pm
8
Thanks, I see.
Here is my code now:
// Setup
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;
}
if(prop === "tracks" && collection.hasOwnProperty("tracks") === false) {
collection.id.tracks = [];
collection.id.tracks.push(value);
}
if(prop === "tracks" && value !== "") {
collection.id.tracks.push(value);
}
if(value === ""){
delete collection[id][prop];
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
I get error: Cannot set property âtracksâ of undefined
sp2012
June 26, 2019, 4:57pm
11
Thanks.
Here is my code now:
// Setup
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" && collection.hasOwnProperty("tracks") === false) {
collection[id].tracks = [];
collection.id.tracks.push(value);
}
else if(prop === "tracks" && value !== "") {
collection[id].tracks.push(value);
}
else if(value === ""){
delete collection[id][prop];
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
I get error: Cannot read property âtracksâ of undefined
sp2012
June 27, 2019, 9:13am
13
Thanks.
Here is my code now:
// Setup
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" && collection.hasOwnProperty("tracks") === false) {
collection[id].tracks = [];
collection[id].tracks.push(value);
}
else if(prop === "tracks" && value !== "") {
collection[id].tracks.push(value);
}
else if(value === ""){
delete collection[id][prop];
}
return collection;
}
// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");
I get errors:
After updateRecords(2468, âtracksâ, âFreeâ), tracks should have â1999â as the first element.
After updateRecords(2548, âtracksâ, ââ), tracks should not be set
ilenia
June 27, 2019, 9:28am
14
this will always be false
sp2012
June 27, 2019, 9:53am
15
Thanks. Why? And, how do I fix that?
ilenia
June 27, 2019, 10:49am
16
heck carefully what you have written. Does collection
have a tracks
property?
sp2012
June 27, 2019, 11:50am
17
It has a tracks property. Only the last object lacks a tracks property.
ilenia
June 27, 2019, 12:03pm
18
are you really sure that collection
has a tracks
property?
so, if you do collection.tracks
you should get something.
sp2012
June 27, 2019, 12:41pm
19
I donât get it, can you please explain? Tracks is in collection[id].tracks.
sp2012
June 27, 2019, 1:40pm
20
I turned this:
into this: if( collection[id].hasOwnProperty(âtracksâ) === false
and now it works.
1 Like