Record Collection Fundamental Problems

if you have only the object, how are you to know which record to change? the instructions are all about which value prop and value have, if you don’t have them how can you know? without id how are you to know which inner object to update?

I thought the information was all inside the passed object (records) or not - then one could check missing properties within the passed object via .hasOwnProperty-function(?).

The id, prop and value are all arbitrary – they are not dependent upon the recordCollection object in any way.

Say you had a collection of music on your computer. If you wanted to update some information for a specific album in that collection, which of these two approaches would work:

Give the computer the collection of music, just let it figure out what you want to do.

Or

Give the computer the collection along with the id of the album you want to update and the updated information.

1 Like

If I gave the computer the whole collection, the IDs should be stored inside the object, so I should be albe to access them through (pseudocode) collectionname.ID or collectionname[ID] (?)
Why do I need to pass the ID additionally to the object (=collection)?

Right. Say you have a collection of ebooks on some ebook device, and some application that manages them.

You want to find a book.

To find the book you want, do you simply give the application the collection of books?

The collection contains the book you want, why would the computer be unable to find it for you?

the ids of the various records are stored there, but if you don’t tell the computer which id to use there is no way it can figure out which record to update

@DanCouper @ilenia
Thanks for your effort, it is getting slightly clearer to me. I think the answer is, if I don’t give the user the chance to input (at least) the collection and the ID, there is no chance that the user will get the desired CD as an output?
The function works with the passed arguments but can’t magically pick the desired CD-ID for the user?

I wouldn’t go overboard with the abstractions. There is no user who is inputting values.

There is a function.

The function has four parameters.

The first parameter is an object that looks like this:

{
  1: {
    artist: 'Tool',
    album: 'Lateralus'
  },
  2: {
    artist: 'Tool',
    album: '10,000 Days'
  }
}

The rest of the parameters tell the function which value to update.

So if the function receives the above object as its first argument, followed by the arguments 2, 'album', 'Fear Inoculum', then the function should return an object that looks like this:

{
  1: {
    artist: 'Tool',
    album: 'Lateralus'
  },
  2: {
    artist: 'Tool',
    album: 'Fear Inoculum' // value updated
  }
}

Does this help you understand the point of the id, prop, and value parameters?

I think I am getting there. The if-statements checking for empty strings seem to be related to cases where no property is in the initial object nested within the array.
Does prop relate to something as “artist” or “album” and value to the content of “artist” and “album”?

Yes, all I was trying to get across is that if you have a collection of stuff and you need to pick one item you need to actually pick an item. Like if you go into a shop for something you have to actually go and look for the the something or ask for the something, the shop itself won’t just magically give you the thing you want.

If you have a function that does the job, the function needs some way to pick the item (ie using an ID)

If you have a function that picks something from an object, it needs to know the key/s of the something you’re trying to pick from the object

if value is an empty string you need to delete the property from the object. remember what the rules say

Thanks you, it helped me a lot to understand!

So in this record collection challenge, the function parameter are a representation of the data structure of the object recordCollection?

How does JS know that I mean the object recordCollection, within the function updateRecords we always refer to it as records and not recordCollection.
When we call the function it says recordCollection.

You don’t always mean recordCollection - it depends upon what arguments you use when you call the function.

What?
So you meanI could use that function on a different object?

I thought the first argument would be always this recordCollection

It will be an object structured like recordCollection. The whole point of an argument is that you can also try other inputs.

yes I understand that.

but in the function body we access the object properties with

records[id][prop] and not recordCollection[id][prop]

Why?

Literally so we can use different records objects instead of being forced to only use recordCollection

The function signature explicitly uses the following arguments, so those should be the only variables you use in your function (other than any created inside of the function):
function updateRecords(records, id, prop, value)

wow really cool! mind blown:D

Thx Jeremy!!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.