Dia 41/2019 - Brincando com array: Includes, IndexOf, LastIndexOf, Find e FindIndex

Desafio #92daysofcode que fiz no final de 2019. Nesse artigo temos o dia 41, mostrando o uso dos métodos: Includes, IndexOf, LastIndexOf, Find e FindIndex.

Pré-visualização

Código

Começamos com a estrutura do HTML.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Day 41</title>
<style>
*, *:before, *:after { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing:border-box; }
html { -webkit-box-sizing:border-box; -moz-box-sizing:border-box; box-sizing:border-box; font-size: 62.5%; }
* { margin: 0; padding: 0; list-style: none; outline: none; }
body { background: #000; color: #fff; font-family: Arial, Helvetica, sans-serif; }
.container { margin: 20px auto; max-width: 992px; width: 100%; }
</style>
<link rel="stylesheet" type="text/css" href="./day-41.css">
</head>
<body>
<div class="container">
<!-- terminal -->
</div>
<script src="./day-41.js"></script>
</body>
</html>
1
2
3
4
5
6
7
<form data-js="form" class="form">
<input type="text" data-js="input" class="input">
</form>
<div class="terminal">
<div data-js="prompt" class="prompt"></div>
</div>
<pre data-js="example" class="example"></pre>
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
.form,
.input { height: 100%; opacity: 0; position: fixed; top: 0; left: 0; width: 100%; }

.terminal { position: relative; padding: 4rem; }

.prompt {
color: #1ff042;
display: block;
font-weight: 700;
text-transform: uppercase;
font-size: 1.4rem;
letter-spacing: 0.15em;
line-height: 1;
margin-bottom: 1rem;
white-space: pre-wrap;
}

.prompt:before { content: '> '; display: inline-block; }

.prompt:after {
animation: _cursorBlink 1.25s steps(1) infinite;
background: #1ff042;
content: '';
display: inline-block;
height: 1em;
margin-left: 5px;
vertical-align: -0.15em;
width: 0.75em;
}

.example { font-size: 2rem; padding: 0; }

@keyframes _cursorBlink {
0% { opacity: 0; }
50% { opacity: 1; }
100% { opacity: 0; }
}
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
const players = [
{name: 'Name 1', age: 18, score: 0, status: 0},
{name: 'Name 2', age: 10, score: 30, status: 1},
{name: 'Name 3', age: 15, score: 0, status: 1},
{name: 'Name 4', age: 15, score: 40, status: 0}
];

const names = players.map(item => item.name);
const scores = players.map(item => item.score);

// Includes
const includes = names.includes('Name 2');
console.log(`Name 2 is ${includes ? `included` : `not included` }`);
// Name 2 is included

// IndexOf
const indexOf = scores.indexOf(0);
console.log(`Position ${indexOf}`, players[indexOf]);
// Position 0 {id: 1, name: "Name 1", age: 18, score: 0, status: 0}

// LastIndexOf
const lastIndexOf = scores.lastIndexOf(0);
console.log(`Position ${lastIndexOf}`, players[lastIndexOf]);
// Position 2 {id: 3, name: "Name 3", age: 15, score: 0, status: 1}

// Find
const find = players.find(item => item.status === 1);
console.log(`First item found:`, find);
// First item found: {id: 2, name: "Name 2", age: 10, score: 30, status: 1}

// FindIndex
const checkAdult = item => item.age >= 18;
const findIndex = players.findIndex(checkAdult);
console.log(`First position found: ${findIndex}`);
// First position found: 0

const prompt = document.querySelector('[data-js="prompt"]');
const form = document.querySelector('[data-js="form"]');
const example = document.querySelector('[data-js="example"]');
const input = document.querySelector('[data-js="input"]');

input.focus();

form.addEventListener('click', () => {
input.focus();
});

input.addEventListener('input', evt => {
let text = '';

switch(evt.target.value) {
case 'includes':
text = `
// Includes
const includes = names.includes('Name 2');
console.log(\`Name 2 is ${includes ? `included` : `not included` }\`);
// Name 2 is included
`;
break;
case 'indexof':
text = `
// IndexOf
const indexOf = scores.indexOf(0);
console.log(\`Position ${indexOf}\`, players[indexOf]);
// Position 0 {id: 1, name: "Name 1", age: 18, score: 0, status: 0}
`;
break;
case 'lastindexof':
text = `
// LastIndexOf
const lastIndexOf = scores.lastIndexOf(0);
console.log(\`Position ${lastIndexOf}\`, players[lastIndexOf]);
// Position 2 {id: 3, name: "Name 3", age: 15, score: 0, status: 1}
`;
break;
case 'find':
text = `
// Find
const find = players.find(item => item.status === 1);
console.log(\`First item found:\`, find);
// First item found: {id: 2, name: "Name 2", age: 10, score: 30, status: 1}
`;
break;
case 'findindex':
text = `
// FindIndex
const checkAdult = item => item.age >= 18;
const findIndex = players.findIndex(checkAdult);
console.log(\`First position found: ${findIndex}\`);
// First position found: 0
`;
break;
default:
text = evt.target.value.indexOf('hemerson') >= 0 ? ' // Nerd Calistenico' : '';
}

prompt.innerHTML = evt.target.value;
example.innerHTML = text;
});

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.