Cant push new item to existing array

Tell us what’s happening:
Hello folks, I have a problem when I want to push new items to the existing array. Can you help me with what wrong with this code?
In my head, I already do it correctly, but the code didn’t like that.

Your code so far


// Setup
var collection = {
2548: {
  albumTitle: 'Slippery When Wet',
  artist: 'Bon Jovi',
  tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
  albumTitle: '1999',
  artist: 'Prince',
  tracks: ['1999', 'Little Red Corvette']
},
1245: {
  artist: 'Robert Palmer',
  tracks: []
},
5439: {
  albumTitle: 'ABBA Gold'
}
};

// Only change code below this line
function updateRecords(object, id, prop, value) {
if(object[id][prop] !== 'tracks' && value !== ''){
  object[id][prop] = value;
} else if(object[id][prop] === 'tracks' && !object[id].hasOwnProperty(prop)){
  object[id].tracks = []
} else if(object[id][prop] === 'tracks' && value !== ''){
  object[id][prop].push(value)
} else if(value === ''){
  delete object[id][prop]
} else {
  console.log('error')
}
object[id][prop].push(value)
return object;
}

updateRecords(collection, 5439, 'artist', 'ABBA');

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36.

Challenge: Record Collection

Link to the challenge:

btw, one line before return object; i do that only for testing.

Fun fact: i try with console and type like this : collection['2468']['tracks'].push('new item');
And it works.

1 Hour+ and now im fixed this, yayy :smiley:

function updateRecords(object, id, prop, value) {
  if (prop !== 'tracks' && value !== ''){
    object[id][prop] = value;
  } else if (prop === 'tracks' && !object[id].hasOwnProperty(prop)){
    object[id].tracks = [];
    object[id][prop].push(value);
  } else if (prop === 'tracks' && value !== ''){
    object[id][prop].push(value);
  } else if(value === ''){
    delete object[id][prop];
  } else {
    console.log('error');
  }
  return object;
}

Your object is not an array, so it has no push() method. You can assign a value to object[id][prop], however.

This isn’t quite right. The method “push” is acting on object[id][prop] which is a string. You can’t push a value to a string they would have to be concatenated.

But the bigger problem is not that you are trying to push to a string but that you didn’t understand the logic well enough to realize that that is not always going to be an array.

The only property you would need to push to would be “tracks”.