Dia 48/2019 - Estruturas de dados em JS: matrizes e objetos

Desafio #92daysofcode que fiz no final de 2019. Nesse artigo temos o dia 48, mostrando o uso de matrizes e objetos.

Código

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
// Array

// Initializing an array
const days = new Array();
console.log(days);
// []
const days1 = new Array(5);
days1[2] = 3;
console.log(days1);
// [empty × 2, 3, empty × 2]
const days2 = new Array(1, 2, 3, 4, 5);
console.log(days2);
// [1, 2, 3, 4, 5]
const days3 = [6, 7, 8, 9, 10];
console.log(days3);
console.log(days3.length);
// [6, 7, 8, 9, 10]
// 5

// Destructuring arrays
const [x, y] = [1, 2];
console.log(x, y);
// 1 2

// Add items to an array with push() and unshift()
const numbers = [3, 4];
numbers.unshift(1, 2);
console.log(numbers);
// [1, 2, 3, 4]
numbers.push(5);
console.log(numbers);
// [1, 2, 3, 4, 5]

// Remove items from an array with pop() and shift()
const items = ['one', 'two', 'three'];
items.pop();
console.log(items);
// ["one", "two"]
items.shift();
console.log(items);
// ["two"]

// Remove items using splice()
const array = ['one', 'two', 'three', 'four', 'five'];
array.splice(2, 2);
console.log(array);
// ["one", "two", "five"]

// Add items using splice()
const colorChange = (arr, index, newColor) => {
arr.splice(index, 1, newColor);
return arr;
}

let colorScheme = ['#878787', '#a08794', '#bb7e8c', '#c9b6be', '#d1becf'];
colorScheme = colorChange(colorScheme, 2, "#332327");
console.log(colorScheme);
// ["#878787", "#a08794", "#332327", "#c9b6be", "#d1becf"] // #bb7e8c to #332327

// Copy an array with slice()
let weatherConditions = ['rain', 'snow', 'sleet', 'hail', 'clear'];
let todaysWeather = weatherConditions.slice(1, 3);
console.log(weatherConditions);
console.log(todaysWeather);
// ["rain", "snow", "sleet", "hail", "clear"]
// ["snow", "sleet"]

// Copy an array with spread syntax
const array1 = [true, true, undefined, false, null];
const array2 = [...array1];
console.log(array1);
console.log(array2);
// [true, true, undefined, false, null]
// [true, true, undefined, false, null]

// Check for the presence of an element with indexOf()
const fruits = ['apples', 'pears', 'oranges', 'peaches', 'pears'];
console.log(fruits.indexOf('dates')); // -1
console.log(fruits.indexOf('oranges')); // 2
console.log(fruits.indexOf('pears')); // 1

// Iterate through all the items in an array
const example = arr => {
const newArr = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] > 10) {
newArr.push(arr[i]);
}
}
return newArr;
}
console.log(example([12, 8, 14, 80, 0]));
// [12, 14, 80]

// Create complex nested arrays
// two-dimensional & multidimensional
const nestedArray = [
['deep'],
[
['deeper'], ['deeper']
],
[
[
['deepest'], ['deepest']
],
[
[
['deepest-est?']
]
]
],
];
console.log(nestedArray[2][1][0][0][0]);
// deepest-est?

nestedArray[2][1][0][0][0] = 'deeper still';
console.log(nestedArray[2][1][0][0][0]);
// deeper still

// Combining arrays with spread syntax
const thisArray = ['three', 'four', 'five'];
const thatArray = ['one', 'two', ...thisArray, 'six'];
console.log(thatArray);
// ["one", "two", "three", "four", "five", "six"]
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Object

// Initializing an object
const user = new Object();
user.name = 'Nerd';
console.log(user);
// {name: "Nerd"}
const user1 = Object.create(null);
user1['name'] = 'Hemerson';
console.log(user1);
// {name: "Hemerson"}
const name = 'Hemerson';
const user2 = {name};
console.log(user2);
// {name: "Hemerson"}
const user3 = {name: 'Item 1', name: 'Item 2'};
console.log(user3);
// {name: "Item 2"}

// Destructuring objects
const {name, age} = { name: 'Hemerson', age: 33 };
console.log(name, age);
// Hemerson 33

// Use the Delete Keyword to Remove Object Properties
const foods = {
apples: 10,
oranges: 2,
plums: 8,
bananas: 3
};

delete foods.oranges;
delete foods.plums;
delete foods.bananas;
console.log(foods);
// {apples: 10}

// Check if an Object has a Property
const users4 = {
nerd: {
age: 33,
online: true
}
};

const hasOwnProperty = (prop, obj) => prop in obj ? true : false;
console.log(hasOwnProperty('nerd', users4));
console.log(users4.hasOwnProperty('nerd'));
// true
// true

// Iterate Through the Keys of an Object with a for...in Statement
const users5 = {
nerd: {
age: 33,
online: false
},
hemerson: {
age: 43,
online: true
},
calistenico: {
age: 23,
online: true
},
};

const countOnline = obj => {
let n = 0;
for (let user in obj) {
if (obj[user].online) {
n++;
}
}
return n;
};

console.log(countOnline(users5));
// 2

// Generate an Array of All Object Keys with Object.keys()
const users6 = {
nerd: {
age: 33,
online: false
},
hemerson: {
age: 43,
online: true
},
calistenico: {
age: 23,
online: true
},
};

const getArrayOfUsers = obj => Object.keys(obj);
console.log(getArrayOfUsers(users6));
// ["nerd", "hemerson", "calistenico"]

// Modify an Array Stored in an Object
const user = {
name: 'Nerd',
age: 33,
data: {
username: 'nerd123',
items: ['book', 'tv']
}
};

const addItems = (user, item) => {
user.data.items.push(item);
return user;
}
console.log(addItems(user, 'computer'));
// { name: 'Nerd', age: 33, data: { username: 'nerd123', items: ['book', 'tv', 'computer'] } }

// Object.assign()
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };

const returnedTarget = Object.assign(target, source);

console.log(target);
console.log(returnedTarget);
// { a: 1, b: 4, c: 5 }
// { a: 1, b: 4, c: 5 }

// Object.is()
const obj1 = {
item1: '1',
item2: '2',
item3: '3',
item4: '4'
}

const obj2 = obj1;
const obj3 = Object.assign({}, obj1);
console.log(Object.is(obj1, obj2)); // true
console.log(Object.is(obj1, obj3)); // false
console.log(obj3);
// {item1: "1", item2: "2", item3: "3", item4: "4"}

// Object.freeze()

const obj4 = {
item1: '1',
item2: '2',
item3: '3',
item4: '4'
}

obj4.item1 = '11';
obj4.item5 = '5‍';

Object.freeze(obj4);

obj4.item1 = '111';
obj4.item6 = '6';
delete obj4.item3;

console.log(obj4);
// {item1: "11", item2: "2", item3: "3", item4: "4", item5: "5‍"}

Conclusão

A postagem original pode ser vista no meu Instagram e o código está acessível no meu Github.

Relacionados

Ao fechar este aviso ou continuar navegando no site Nerd Calistênico, você aceita o uso de cookies.

Este site usa cookies para assegurar a melhor experiência para os nossos usuários. Consulte nossa política de privacidade.

Uma nova versão está disponível. Clique aqui para atualizar.