JS: Iterating through properties in a conditional statement

Hello. I’m having a bit of trouble with the exercise “Record Collection”. Here’s my first attempt at it:


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"
    }
}; 

function updateRecords(id, prop, value) {
      if(prop == "tracks" && value !== "" ) {
        if(collection[id].tracks == undefined) {
          var emptyArray = [];
          collection[id].tracks = emptyArray;
          collection[id].tracks.push(value);
        } else collection[id].tracks.push(value);
        } 
        
        if(prop != "tracks" && value !== "") {
        collection[id].prop = "";
        } else if(value == "") { 
        delete collection[id].prop;
      }
return collection; 
}

console.log(updateRecords("1245", "artist", "Random")); returned "{ 1245: { artist: 'Robert Palmer', tracks: [], prop: '' }", instead of changing the name of the artist. I could work my way out of this and pass the exercise by writing another function, assigning a separate conditional statement to each property, like this:

function updateRecords(id, prop, value) {
      if((prop == "tracks" && value !== "") && (collection[id].tracks == undefined)) {
          var emptyArray = [];
          collection[id].tracks = emptyArray;
          collection[id].tracks.push(value);
        } else if (prop == "tracks" && value != "" ) {
          collection[id].tracks.push(value);
        } else if (prop == "tracks" && value == "" ) {
          delete collection[id].tracks;
        }
      
      if(prop == "artist" && value == "") {
        delete collection[id].artist;
      } else if (prop == "artist" && value != "") {
        collection[id].artist = value;
      }

      if(prop == "album" && value == "") {
        delete collection[id].album
      } else if (prop == "album" && value != "") {
        collection[id].album = value;
      }
return collection; 
} 

However, I wasn’t happy with this solution because I thought there must’ve been another way to accomplish this without having to write explicit “if” statements for each property—which is kind of overkill, I guess.

Just to test the core idea of my first example (accessing an object’s property inside a conditional statement), I thought of writing a simple function that would delete a property if the “value” parameter were empty:

function updateRecords2(id, prop, value) {
    if(value == "") {
      delete collection[id].prop;
    }
    return collection; }

But it still didn’t work. So I’m wondering if there’s some way to do what I want?

Thank you.

good start - remember though that you can’t use dot notation with variables…


same issue with notation, but fixed that, note that you are setting the value of the property to an empty string. never giving it a value
you should set it to the provided value instead


now, the hard part is the tracks part
remember that in an if/else if/else chain only the first statement with a true condition is executed, the others are not. you need to review the part for when prop is tracks
remember that you can’t push to an array that doesn’t exist, but if the array does exist you should not be deleting what’s already in there

Can you not use a switch statement instead?

I didn’t know about the dot notation. Yeah, my first example was messed up; at least my second passed the test. I just wanted to know if there was a better way to do it, without having to write the name of each property for each if clause.

I rewrote it with switch, according to abruzzi1’s suggestion, and I think it looks cleaner:

function updateRecords(id, prop, value) {
    if(value == "") {
      delete collection[id][prop];
    } else switch(prop) {
    
    case "tracks":
    if(collection[id].tracks == undefined) {
        var emptyArray = [];
        collection[id].tracks = emptyArray;
        collection[id].tracks.push(value);
      } else collection[id].tracks.push(value);
    break;

    case "artist":
      collection[id][prop] = value;
      break;

    case "album":
      collection[id][prop] = value;
      break;
    }
return collection; }

Is there any other way to do it? Like, with a single if statement instead of switch? Or is this just a better way?

TY

yes, use bracket notation to access the property

you are doing it, so you don’t need to use the switch statement like that…

collection[id][prop] = value will work, you don’t need to put a condition for each value that prop could have. note that you wrote the exact same thing in the switch cases - maybe that means you don’t need the switch ?

I rewrote it:

function updateRecords(id, prop, value) {
  if(value == "") {
    delete collection[id][prop];
  } else {
    if(prop == "tracks" && collection[id].tracks == undefined) {
      var emptyArray = [];
      collection[id].tracks = emptyArray;
      collection[id].tracks.push(value);
    } else if (prop == "tracks" && collection[id].tracks) {
      collection[id].tracks.push(value);
    } else if (prop != "tracks") {
      collection[id][prop] = value;
    } }
  
  return collection;
}

I think it’s better now. Is it ok to write if/else-if statements nested inside the else, though?

nested if/else if correct, and now it is
much better, good job

1 Like