Record Collection Exercise. Is there a way to do it by accessing the Array instead of If/Else statement?

Tell us what’s happening:

Is there a way I can do this exercise by accessing the array, instead of doing in with If/Else statement? How would that be? I was trying to do it that way before I saw the solution.

You can see I also tried to use “var collection = delete [“collection”][“2548”][“artist”][“Bon Jovi”];” I tried all of different forms but it just didn’t work. Why? If there’s a way to do that how would that be? Is it possible? If not possible, why?

Here is the Exercise: https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection

Here’s the solution with If/Else statement: https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-javascript/record-collection/

Your code so far


// Setup
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"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function updateRecords(id, prop, value) {
  
  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",
      "artist": "ABBA",
      "tracks": "Take a Chance on Me"
    }
};

 return collection
  
 var collection = delete ["collection"]["2548"]["artist"]["Bon Jovi"];

}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/record-collection

You don’t need to declare “collection” inside the function. It has been already declared for you. Your code “var collection = delete [“collection”][“2548”][“artist”][“Bon Jovi”] doesn’t make sense. If you want to delete the property “artist” from id “2548” of the collection you have to write like this delete collection[“2548”][“artist”]. Note that delete something returns true or false.

This isn’t valid JS:

var collection = delete ["collection"]["2548"]["artist"]["Bon Jovi"];

In this case, you access properties like collection['2548']. collection is the identifier that the object is assigned to. This isn’t a thing: ['collection'].

And you can’t assign this: collection = delete {something}. delete is an operation on an object (it deletes a property), it makes no sense trying to assign it to something.

It put this in the code but it doesn’t work to complete the request. So, there’s no way of deleting the property by accessing the array that way, without using if/else statement?

const myObj = {
 foo: 1,
 bar: 2
}

delete myObj['foo']

myObj // myObj is now { bar: 2 }

Do you mean object rather than array, or are you atually trying to delete things in the array in that object?

For example, the exercise asks:

“After updateRecords(1245, "tracks", "Addicted to Love") , tracks should have "Addicted to Love" as the last element.”

But when I add this code:

var collection = collection[“1245”][tracks].push

it doesn’t work.

For example, the exercise asks:

“After updateRecords(1245, "tracks", "Addicted to Love") , tracks should have "Addicted to Love" as the last element.”

But when I add this code:

var collection = collection[“1245”][tracks].push

it doesn’t work.

Again, syntax is wrong. push is a function that takes the thing you want to push into the array. myArray.push(1) pushes the value 1 into myArray.

And push, like delete, works as an operation. It returns a value, but that’s the new length of the array. So if you do adjust your code so push is correct, what you will be doing with collection = someArray.push(someValue) is overwriting collection with a number