DEV Community

Daniel Valle
Daniel Valle

Posted on

Objects and Arrays

Objects IronHack Document

Objects are commonly used as a median to assign larger amounts of data

Objects are a collection of properties and those properties are represented by key Value pairs

A key-value pair is a string that identifies as a property and corresponds to a value.

let weeklySchedule = {
monday: 7-12
tuesday: 7-11
wednesday: 7-4
thursday: 7-3
friday: 7-9
}
Enter fullscreen mode Exit fullscreen mode

"monday", "tuesday" etc.. are the keys that are assigned number values using ":"

In the "weeklyschedule" object I used object literal syntax, i used "{}" to create the object.

Accessing the Values in Objects, in order to display the values inside an object you must log it into the console like so:

console.log(weeklyschedule);
Enter fullscreen mode Exit fullscreen mode

Changing property values

We can change the value of a property after it is defined using dot notation:

let workHours = {
mon: 8-4
tues:8-4
wed: 10-4
thurs:8-4
fri:8-4
}
workHours.wed = 12-4;
Enter fullscreen mode Exit fullscreen mode

Another method of changing values is bracket notation:

let workHours = {
mon: 8-4
tues:8-4
wed: 10-4
thurs:8-4
fri:8-4
}
workHours[tues] = 12-8;
Enter fullscreen mode Exit fullscreen mode

The "in" operator, this operator can be used to determine if a certain property exists in an object:

let workHours = {
mon: 8-4
tues:8-4
wed: 10-4
thurs:8-4
fri:8-4
}
'mon' in workHours // true
'sat' in workHours // false
Enter fullscreen mode Exit fullscreen mode

You can remove properties using "delete":

let favoriteCarBrand = {
make1: 'Ferrari'
make2: 'Honda'
make3: 'Nissan'
}
delete favoriteCarBrand.make1 // dot method
delete favoriteCarBrand['make1'] // bracket method
Enter fullscreen mode Exit fullscreen mode

Listing Properties

One method is "Object.keys()", another method is "for...in loop":

let favoriteCarBrand = {
make1: 'Ferrari'
make2: 'Honda'
make3: 'Nissan'
}
Object.keys(favoriteCarBrand) // object.keys method
for(let key in favoriteCarBrand){
console.log(key); // for in loop method
}
Enter fullscreen mode Exit fullscreen mode

Arrays and Objects IronHack Document

Arrays and Objects are some examples of data structures, a data structure is a specific way of storing data

In certain situations you can use arrays inside of objects to increse productivity:

let finalProjectGroups = {
['John', 'Alyssa', 'Robert']
['Isabella', 'Dylan', 'Alejandro'] 
['Jorge', 'Nicolas', 'Daniel']
}
Enter fullscreen mode Exit fullscreen mode

Some objects are known as complex objects for the amounts of data they hold for example:

let peopleInTennisLessons = {
{ firstname:'Alejandro', lastname;'Rodriguez' age: 24 }
{ firstname:'Alyssa', lastname;'Garcia' age: 21 } 
{ firstname:'Alex', lastname;'Garcia' age: 15 }
{ firstname:'Darian', lastname;'Alexi' age: 18 }
Enter fullscreen mode Exit fullscreen mode

Objects inside of Objects

When you create an object in an object the way you access it changes, lets create an object inside an object and access it:

let virtualClassroom2 = {
teacher: { firstname:'Alberto' lastname:'Garcia' age:35
};
console.log(virtualClassroom2.teacher) / * this shows all values in "teacher" * /
console.log(virtualClassroom2.teacher.age) / * this is how you access values in an object inside of an object * /
}
Enter fullscreen mode Exit fullscreen mode

Udemy Net Ninja Section 5

Top comments (0)