Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
var recordCollection = {
  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(records, id, prop, value) {
  if (prop!= 'tracks' && value!='') {
    records[id][prop]=value;
  }else if (prop==='tracks' &&records[id].hasOwnProperty('tracks')===false) {
    record[id][prop]=[value];
  }else if(prop==='tracks' &&value!="") {
    records[id][prop].push(value);
  }else if(value==="") {
    delete records[id][prop];
  }
return records
  
}

in recordCollection object tracks is a array containing string.

What I have tried:

i tried using tracks instead of using "tracks" an thus a eror came that tracks is undefined
Posted
Updated 19-May-21 8:45am
v2

1 solution

Because the prop parameter contains the name of the property at hand and the name is a string. This is why you need to compare it to another string (literal in this case).
 
Share this answer
 
Comments
Aurangajeb Azad 24-May-21 6:00am    
but tracks is already declared in the object
Wendelius 24-May-21 10:47am    
It's a value of an object, not an object by itself. So you cannot reference tracks alone without specifying the object. However, you can investigate if the property is named tracks
Aurangajeb Azad 4-Jun-21 5:25am    
could you give an example?
Wendelius 4-Jun-21 5:29am    
You already have an example in your code. The line

if (prop!= 'tracks'...

checks if the property is named 'tracks' or not.

If you mean how the names are found out, there are multiple ways and situations, but have a look at for example Object.getOwnPropertyNames() - JavaScript | MDN[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900