Basic JavaScript - Record Collection


Tell us what’s happening:
Describe your issue in detail here.

I have three questions.
① I can’t understand the meaning of 「records[id][prop] = value;」.

I know this part applies to “update or set that album’s prop to value .” in the instruction.
But I thought if I want to access the parameter 「prop」, I should type 「updateRecords[prop]」.

②I can’t understand the meaning of 「resords[id]」either. i think that’s because I don’t get the relationship of (records, id, prop, value). Why is only records black?

③ I don’t understand the difference between the third blue highlight and first. How does [value] work in this code?

I am shameful about the lack of understanding. I really appreciate if someone offers help.

it seems you missed two questions

but for number one:
look at updateRecords, what are its properties? does it have properties such as ‘tracks’, ‘artist’, etc, or are those on the inner objects?

1 Like

In the recordCollection, the property 2468:

  2468: {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  },

can be accessed as: recordCollection['2468']

And the property tracks within that record id can be accessed as: recordCollection['2468']['tracks']

In the updateRecords function, the value of records is recordCollection, the value of id can be 2468, the value of prop can be one of artist, tracks, etc., and the value of value can be any string, e.g., "some value".

NOTE that your code needs to be with the function updateRecords.

1 Like

Thanks for your detailed explanation. However I can’t seem to be able to understand yet.
1.Are those arguments same hierarchy?
2. Why do we need [ id ] between records and [prop]?
These questions are haunting me…:sob:

Have you tried console.loging the values of the function arguments? That can be very informative.

It is amazing that you were able to solve this problem without understanding what any of the arguments mean.

There are two aspects to this question, I think, you need to know. First, how do you access the value of a property, for an object? Next, what is a function, and what are the various aspects of a function?

When you are clear about these, you will know more about the issue you are facing (and answers).

1 Like

Hi, don’t know if you still need help, but your delete should come before push value. just like this
}else if (value === “”) {
delete records[id][prop]

}else if (prop === “tracks”) {
records[id][prop].push(value);
}

That should not make any difference as the code is written in the original post.

Thank you for following explanation. After posting here, I also asked about this in a different website. And then I got very clear diagram to understand the structure. So now I think I’ve got better understanding about this question.

image
To answer the first, I’ve learned that [id]and [prop]are in different hierarchy, meaning I have to use two .
For the second, I basically consider function as group of data. And its data can be changed by declaration.

I obviously know this understanding has lots of missing, but I just want to check if current state is in right direction.

I try to explain few things here:

The recordCollection is an object; a JavaScript object. In general, an object has properties and each property has a value.

PART 1:

let anObject = { prop1: "val1", prop2: 23 };
How do I access a property’s value? For example,
let prop1Value = anObject.prop1;
Now the variable prop1Value has a value of "val1".

The same above statement can also be written using the bracket notation:
let prop1Value = anObject["prop1"];

Lets define another object:

let anotherObject = {
    prop3: 99.95,
    prop4: {
        prop41: "hello",
        prop42: "world"
    }
}

What do we get when we access the the value of the property prop4?

let prop4Value = anotherObject.prop4;
// or
let prop4Value = anotherObject["prop4"];

Now, the variable prop4Value has this value, { "prop41" : "hello", "prop42" : "world" }, and it is an object.

Again, how do we access the value "hello" (using the property prop41)?

let prop41Value = prop4Value.prop41;
// or
let prop41Value = prop4Value["prop41"];

Now the variable prop41Value has a value of "hello".

Before, going further, be very clear about the above discussion.

PART 2:

Now, lets access the prop41’s value in a different way.

let prop41Value = anotherObject["prop4"]["prop41"];

Note that this is a combination of the statements in the PART 1 of the discussion:

let prop4Value = anotherObject["prop4"];
let prop41Value = prop4Value["prop41"];

PART 3:

Lets take this statement from PART 2:

let prop41Value = anotherObject["prop4"]["prop41"];

Define two variables:

let prop_four = "prop4";
let prop_forty_one = "prop41";

Now, the above statement can be written as:

let prop41Value = anotherObject[prop_four][prop_forty_one];

The result is "hello" ( the variable prop41Value has a value of "hello").

PART 4:

Now, update or set the object’s property to a new value, "HELLO!".

anotherObject[prop_four][prop_forty_one] = "HELLO!";

Now, the anotherObject has changed to (compare with the original one in PART 1):

{
    prop3: 99.95,
    prop4: {
        prop41: "HELLO",    // <======== Changed property
        prop42: "world"
    }
}

I would also suggest you read the MDN article

1 Like

Thank you for your suggestion :slight_smile: I’m gonna catch up mu basic defect.

I really appreciate your kindness to give the shirt off your back to a stranger. Clear explanation and color effect helped me understand a lot !!!