-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProperties.js
More file actions
135 lines (107 loc) · 4.01 KB
/
Properties.js
File metadata and controls
135 lines (107 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* Objects have properties, each have a label with an associated value. The object literal syntax:
let car = {}
Allows me to define a 'car' object with a property name labeled 'color' with a 'value' of blue.
let car = {color: 'blue'}
Labels can be any string. Quotes were not required for the label color. If I wanted to include spaces, hyphens,
or other special characters in the property name, I will need quotes. Each property is separated by a comma.
You set the value of a property when you define the object.
let car = {'the color': 'blue',
color: blue}
Attempting to access an nonexisting property results in an undefined error.
Sometimes it is useful to check if the property of a given object exists or not. We can use the
.hasOwnProperty(propname) method of objects to determine if that object has the given property name.
.hasOwnProperty() returns true or false if the property is found or not.
Example
var myObj = {
top: "hat",
bottom: "pants"
};
myObj.hasOwnProperty("top"); // true
myObj.hasOwnProperty("middle"); // false
*/
//2 different syntaxes can be used to retrieve value of a property, dot notation and square brackets
let car = {color: 'blue',
'the color': 'deep blue'
}
console.log(car.color)
console.log(car['the color'])
//Objects can have nested objects as properties. Each property is separated by a comma
let car2 = {
brand: {
name: 'Ford'
},
color: 'blue'
}
let brandName = car2.brand.name //dot notation
let brandName2 = car2['brand']['name'] //square brackets
console.log(brandName)
console.log(brandName2)
//can also mix dot notation and square brackets
car2.brand['name']
car2['brand'].name
//can update the value of property after it is defined.
let car3 = {color: 'red'} //defining property and setting value
console.log(car3.color)
car3.color = 'pink' //updating property value using dot notation
console.log(car3.color)
car3.color = ['hot magenta']//updating property value using square brackets
console.log(car3.color)
//can also add a new property to object.
car3.model = 'Buick' //adding new property to object
console.log(car3)
//can also remove a property from an object
let car4 = {
color: 'blue',
brand: 'Ford'
}
console.log(car4)
delete car4.brand //removing the brand property from the car4 object
console.log(car4)
//square brackets also works = delete car['brand']
/* Remove a property without mutating the object:
If mutability is a concern, can create a completely new object by copying all the properties from the old,
except the one you want to remove.
*/
let carCopy = {
color: 'blue',
brand: 'Ford'
}
let prop = 'color'
let newCar = Object.keys(car).reduce((object, key) => {
if (key !== prop) {
object[key] = car[key]
}
return object
}, {})
/* Counting the number of properties in an object:
Use the Object.keys() method, passing the object you want to inspect, to get an array of all the enumerable
properties of the object. (Enumerable properties are those properties whose internal enumerable flag is set to
true, which is the default for properties created via simple assignment or via a property initializer.)
Then calculate the length of that array by checking the length property:
*/
let propCount = {
color: 'Blue',
brand: 'Ford',
model: 'Fiesta'
}
let count = Object.keys(propCount).length
console.log(count)
/* Use the typeof operator. to Check if an object property is undefined.
typeof returns a string that tells the type of the operand. It is used without parentheses, passing it any
value you want to check.
*/
let list = []
let count2 = 2
console.log(typeof list) //"object"
console.log(typeof count2) //"number"
console.log(typeof "test") //"string"
//if value is not defined, typeof returns the 'undefined' string
console.log(typeof color) //"undefined"
//checking if a property is defined in an object
let jeans = {
brand: 'Gap'
}
if (typeof jeans.brand === 'undefined') {
console.log('undefined')
}
else (console.log('defined'))