Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8e05e1a

Browse files
author
Meriéli Manzano
committed
aula 141 a 147
1 parent e5e16a5 commit 8e05e1a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+253
-2
lines changed

‎Array/concat.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Concat, concatena varios arrays em um novo e unico array, com a possibilidade de adicionar novos elementos na concatenação
2+
const filhas = ["Ualeskah", "Cibalena"];
3+
const filhos = ["Uoxiton", "Uesclei"];
4+
5+
const todos = filhas.concat(filhos);
6+
console.log(todos, filhas, filhos);
7+
8+
//Adicionando um elemento extra na concatenação dos arrays
9+
const todosExtra = filhas.concat(filhos, "Fulano");
10+
console.log(todosExtra);
11+
12+
//adicionando arrays e elementos a um array vazio
13+
console.log([].concat([1, 3], [3, 4], 5, [[6, 7]])); //ao usar uma matriz, um array dentro de outro array ele será concatenado como um array dentro do array geral
14+
console.log(["a", "b"].concat([1, 3], [3, 4], 5, [[6, 7]]));

‎Array/flatMap.js‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// flatMap - map associado com concat
2+
const escola = [
3+
{
4+
nome: "Turma M1",
5+
alunos: [
6+
{
7+
nome: "Gustavo",
8+
nota: 8.1,
9+
},
10+
{
11+
nome: "Ana",
12+
nota: 9.3,
13+
},
14+
],
15+
},
16+
{
17+
nome: "Turma M2",
18+
alunos: [
19+
{
20+
nome: "Rebeca",
21+
nota: 8.9,
22+
},
23+
{
24+
nome: "Roberto",
25+
nota: 7.3,
26+
},
27+
],
28+
},
29+
];
30+
31+
const getNotaDoAluno = (aluno) => aluno.nota;
32+
const getNotasDaTurma = (turma) => turma.alunos.map(getNotaDoAluno);
33+
34+
const notas1 = escola.map(getNotasDaTurma);
35+
console.log(notas1);
36+
37+
Array.prototype.flatMap = function (callback) {
38+
return Array.prototype.concat.apply([], this.map(callback));
39+
};
40+
41+
const notas2 = escola.flatMap(getNotasDaTurma);
42+
console.log(notas2);

‎Array/imperativoVSdeclarativo.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const alunos = [
2+
{ nome: "João", nota: 7.9 },
3+
{ nome: "Maria", nota: 9.2 },
4+
];
5+
6+
//imperativo
7+
// Descreve como tem que ser feito
8+
let total1 = 0;
9+
for (let i = 0; i < alunos.length; i++) {
10+
total1 += alunos[i].nota;
11+
}
12+
13+
console.log(total1 / alunos.length);
14+
15+
// Declarativo (melhor opção para reuso de código, e entendimento do todo)
16+
// Descreve o que tem que ser feito
17+
const getNota = (aluno) => aluno.nota;
18+
const soma = (total, atual) => total + atual;
19+
const total2 = alunos.map(getNota).reduce(soma);
20+
21+
console.log(total2 / alunos.length);

‎Array/map2.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
// Desafio Minha resolucao:
21
const carrinho = [
32
'{ "nome": "Borracha", "preco": 3.45 }',
43
'{ "nome": "Caderno", "preco": 13.90 }',
54
'{ "nome": "Kit de Lapis", "preco": 41.22 }',
65
'{ "nome": "Caneta", "preco": 7.50 }',
76
];
87

8+
// Desafio Minha resolucao:
99
const precos = carrinho.map(function (e) {
1010
let objeto = JSON.parse(e);
1111
return objeto.preco;
1212
});
13-
1413
console.log(precos);
1514

1615
// Resolucao da Cod3r

‎Array/map3.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// implementando outro map
12
Array.prototype.map2 = function (callback) {
23
const newArray = [];
34
for (let i = 0; i < this.lenght; i++) {

‎Array/reduce1.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Reduce é usado para reduzir e agregar os valores do array para um único resultado
2+
const alunos = [
3+
{ nome: "João", nota: 7.3, bolsista: false },
4+
{ nome: "Maria", nota: 9.2, bolsista: true },
5+
{ nome: "Pedro", nota: 9.8, bolsista: false },
6+
{ nome: "Ana", nota: 8.7, bolsista: true },
7+
];
8+
9+
console.log(alunos.map((a) => a.nota));
10+
//Somando todos os valores de um array:
11+
const resultado = alunos
12+
.map((a) => a.nota)
13+
.reduce(function (acumulador, atual) {
14+
// o acumulador pode ser um array, um objeto, diversos tipos
15+
console.log(acumulador, atual);
16+
return acumulador + atual;
17+
});
18+
19+
// Somando todos os valores de um array considerando um valor inicial de 10 para a soma
20+
const resultado2 = alunos
21+
.map((a) => a.nota)
22+
.reduce(function (acumulador, atual) {
23+
console.log(acumulador, atual);
24+
return acumulador + atual;
25+
}, 10);
26+
27+
console.log(resultado);
28+
console.log(resultado2);

‎Array/reduce2.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const alunos = [
2+
{ nome: "João", nota: 7.3, bolsista: false },
3+
{ nome: "Maria", nota: 9.2, bolsista: true },
4+
{ nome: "Pedro", nota: 9.8, bolsista: false },
5+
{ nome: "Ana", nota: 8.7, bolsista: true },
6+
];
7+
8+
//Desafio 1: Retornar true/false para verificar se todos os alunos são bolsistas
9+
const todosBolsistas = (resultado, bolsista) => resultado && bolsista;
10+
const desafio1 = alunos.map((aluno) => aluno.bolsista).reduce(todosBolsistas);
11+
12+
console.log("Todos os alunos são bolsistas? " + desafio1);
13+
14+
//Desafio 2: Retornar true/false para verificar se algum aluno é bolsista
15+
const algumBolsista = (resultado, bolsista) => {
16+
console.log("desafio2: " + resultado, bolsista);
17+
return resultado || bolsista;
18+
};
19+
const desafio2 = alunos.map((aluno) => aluno.bolsista).reduce(algumBolsista);
20+
21+
console.log("Algum aluno é bolsista? " + desafio2);

‎Array/reduce3.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Array.prototype.reduce2 = function (callback, valorInicial) {
2+
const indiceInicial = valorInicial ? 0 : 1;
3+
let acumulador = valorInicial || this[0];
4+
for (let i = indiceInicial; i < this.length; i++) {
5+
acumulador = callback(acumulador, this[i], i, this);
6+
}
7+
return acumulador;
8+
};
9+
10+
const soma = (total, valor) => total + valor;
11+
const nums = [1, 2, 3, 4, 5, 6];
12+
console.log(nums.reduce2(soma));
13+
console.log(nums.reduce2(soma, 21));
86 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /