im following fcc youtube on js im trying to create an object, what is supposed to happen is that if a value is empty it should delete the property, is author does not exist it should add the property, i am fairly new to objects so i think i lack certain understanding below is the code:
var collection = {
52789: {
'artist': 'ub40',
'title': 'red red wine',
'year': '1995'
},
56783: {
'artist': 'take that',
'title': 'only takes a minute',
'year': ''
},
54903: {
'artist': 'mc hammer',
'title': 'pray'
}
}
function getCollection(id, property, value) {
if(collection[id][property][value] === ''){
delete collection[id][property][value];
} else if(collection[id][property][value] !== collection[id][property] ['author']) {
collection[id][property][value] = collection[id][property]['author'];
}
return collection;
}
var clonecollection = JSON.parse(JSON.stringify(collection));
console.log(getCollection(id, property, value));
Uhm… where are you setting id
, property
and value
?
ILM
February 7, 2019, 2:32pm
3
No, the condition is “if value
is an empty string”, value
is the parameter of the function.
What do you expect collection[id][property][value]
to be? It doesn’t exist
ILM
February 7, 2019, 2:36pm
4
salv236:
var collection = { 52789: { 'artist': 'ub40', 'title': 'red red wine', 'year': '1995' }, 56783: { 'artist': 'take that', 'title': 'only takes a minute', 'year': '' }, 54903: { 'artist': 'mc hammer', 'title': 'pray' } }
collection[54903]
is { 'artist': 'mc hammer', 'title': 'pray' }
collection[54903]["artist"]
is "mc hammer"
There is not a level you can reach with
collection[id][property][value]
ok here is another attempt, although i am still not comprehending why its not functioning:
var collection = {
52789: {
‘artist’: ‘ub40’,
‘title’: ‘red red wine’,
‘year’: ‘1995’
},
56783: {
‘artist’: ‘take that’,
‘title’: ‘only takes a minute’,
‘year’: ‘’
},
54903: {
‘artist’: ‘mc hammer’,
‘title’: ‘pray’
}
}
function getCollection(id, property, value) {
if(value === ‘’){
collection [id][property] = collection [id][property] || [];
collection[id][property].push(value);
}
return collection;
}
var clonecollection = JSON.parse(JSON.stringify(collection));
console.log(getCollection(56783, ‘year’, 1978));
ILM
February 7, 2019, 9:42pm
6
Please, can you format your code using backticks? There is the button in the post editor </>
You may want to review the challenges about objects and about assigning a value to a property
Didn’t you need to delete the property is value is empty?
Can you please comment your code with what you think each line is doing/should do?
It is difficult to know what you want to do here