OBJECTS Flashcards

1
Q

What is an Object?

A

An object is a collection of properties stored together in one variable, where each property consists of a key-value pair.

An object is represented by a set of curly braces {}.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a key in a object?

A

key is the name of a property; also called a property name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a value in a object?

A

value is the content associated with the key.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What type can be the associated value ?

A

The associated value can be of any type: string, number, boolean, array, function and even another object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why use objects?

A

Objects are useful to group values that belong together into a single unit.

Your code will be much clearer and easier to understand if you use objects to group values. Storing and encapsulating data within objects makes the code much more understandable and easier to maintain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to create an object ?

A

Creating objects is super easy - all you need to do is use a pair of curly braces { and }, like this:

// Create an empty object
const objectName = { };

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to create an object with properties?

A

A more common action is to create an object that contains properties (key-value pairs). To do this, use a pair of curly braces { and } and add your keys and values to it separated by commas:

const objectName = {
myText: “abc”, // key - myText, value - “abc”
myNum: 123 // key - myNum, value - 123
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to access object properties?

A

We use properties (key-value pairs) to store the values in objects. The properties stored in an object can be accessed in two different ways:

Using the dot notation .
Using the bracket notation []

Dot Notation

Syntax: objectName.key

To access a value stored in an object, we use the . followed by the name of the property. This syntax for accessing object values is called dot notation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the other way to access values ? (apart from the dot notation)

A

Bracket Notation

Syntax: objectName[“key”]

Another way to access an object’s property is to use the bracket notation [].

To access a property with bracket notation, we use the [] and a string with the name of the property enclosed within the curly brackets.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to add a new properties to an existing object?

A

As you saw before, you can create as many properties when declaring an object. All you need to do is separate each property (key-value pair) with a comma (,).

Additionally, we may add new properties to an object after declaring it.

To add a new property to an existing object, write a new property name (key) you wish to add, and assign it a value. You may use either dot notation or bracket notation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to update a propertie?

A

At times we may need to update a property existing on an object.

To update a property of an object, specify the name (key) of the property (key) you wish to update, and assign it a new value. You may use either dot notation or bracket notation

student4.city = “London”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Removing Properties

A

Sometimes the structure of our objects changes and we have to remove some of the properties.

To remove a property existing on an object, use the operator delete, followed by the name of the object and the property you want to delete. Example:

delete student5.email;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is list Object.keys for ?

A

This is extremely useful when you work with a huge object and you are not sure what properties it has. Object.keys() receives an object as an argument.

The method Object.keys() returns an array with all the keys of the object. Once you have the array, you can use a for or while loop as seen in the previous lesson, to iterate over the elements and do whatever you please, like listing the values, updating, or even deleting them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is list Object.values for ?

A

JavaScript provides an additional method Object.values(), which you can use to extract the values of all the object properties. This may be useful when you only need the values, excluding the object keys.

The method Object.values() receives an object as an argument.

The method Object.values() returns an array with all the values of the object. Once you have the array with values, you can iterate over it using either a for or a while loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly