Last active
August 29, 2015 14:22
-
-
Save pablomdo/91fc29d7f5aa9d0337b8 to your computer and use it in GitHub Desktop.
Funções nativas de Array que podem ser muito úteis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pares = function (item) { | |
// Retorna apenas números pares | |
return typeof item === 'number' && item % 2 === 0; | |
}; | |
[1, 2, 3, 4, 5, 6].filter(pares); // [2, 4, 6] | |
// Equivalente | |
var pares = [] | |
, arr = [1, 2, 3, 4, 5, 6]; | |
for (var i = 0, length = arr.length, i < length; i++) { | |
if (typeof item === 'number' && arr[i] % 2 === 0) { | |
pares.push(arr[i]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cachorros = function (item) { | |
return { | |
nome: item | |
}; | |
}; | |
['barbas', 'rufus'].map(cachorros); // [{ nome: 'barbas' }, { nome: 'rufus' }] | |
// Equivalente | |
var cachorros = [] | |
, cachorro | |
, arr = ['barbas', 'rufus']; | |
for (var i = 0, length = arr.length, i < length; i++) { | |
cachorro = { name: arr[i] }; | |
cachorros.push(cachorro); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var somar = function (anterior, atual) { | |
return anterior + atual; | |
}; | |
[1, 2, 3].reduce(somar); // 6 | |
// Equivalente | |
var total = 0 | |
, arr = [1, 2, 3]; | |
for (var i = 0, length = arr.length, i < length; i++) { | |
total += arr[i]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Cria um array de objetos com a | |
// propriedade pessoa | |
var pessoas = function (idade, index) { | |
return { | |
nome: 'Pessoa #' + index, | |
idade: idade | |
}; | |
}; | |
// Retorna true se todos os objetos tiverem | |
// idade >= 18 | |
var podemBeber = function (item) { | |
return item && item.idade >= 18; | |
}; | |
// Cria um array de pessoas a partir do map e em seguida | |
// verifica se todos são maiores de 18 | |
[34, 23, 66, 18, 90].map(pessoas).every(podemBeber); // true | |
[40, 15, 51, 20, 42].map(pessoas).every(podemBeber); // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var temNull = function (item) { | |
return item === null; | |
}; | |
['illuminati', 'mascada', 'infinita'].some(temNull); // false; | |
[true, null, 1000].some(temNull); // true; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment