Help with Record Collection

Hi i don’t know what im doing wrong… Error: After updateRecords(collection, 2468, “tracks”, “Free”) tracks should have 1999 as the first element.

//Code


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'

    }

  };

function updateRecords(object, id, prop, value) {

  if (value==""){

    delete object[id][prop];

    return object;}

  if (prop!="tracks"){

    object[id][prop]=value;}

  else if (prop=="tracks" && object.hasOwnProperty(prop)){

    object[id].prop.push(value);}

  else if(prop=="tracks" && object.hasOwnProperty(prop)==false){

    object[id][prop]=[];

    object[id][prop].push(value);

  }

  return object;

}

Please post your code, not an image.


When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Look closely at the code block handling prop=="tracks" && object.hasOwnProperty("tracks") - where is the value getting pushed?

There is a significant difference between these:

object[id].prop
object[id][prop]

my mistake… but got the same error

check this expression

Remove this line.
Here you are assigning empty array to “tracks” array. So it is initialized with zero elements and then you are pushing “value” to it so you are getting only one element in “tracks” array.

1 Like

thank you thats the problem…

object[id].hasOwnProperty(prop) solution!

2 Likes