Abra o console!
Reproduzindo métodos de Array
forEach
function callbackForEach(element, i, array) {
return arrayVazio.push(array[i] = element * 2);
}
function forEach(array, callback) {
for (let i = 0; i < array.length; i++) {
callback(array[i], i, array)
}
}
forEach(arrayForEach, callbackForEach)
find
function callbackFind(element, i, array) {
return element > 2 && element < 12
}
function callbackFindTwo(element, i, array) {
return element > 15
}
function find(array, callback) {
for (let i = 0; i < array.length; i++) {
if (callback(array[i]) === true) {
return array[i]
}
}
return undefined
}
find(arrayFind, callbackFind)
find(arrayFind, callbackFindTwo)
indexOf
function indexOf(array, elementoPesquisa) {
for(let i = 0; i < array.length; i++) {
if(array[i] === elementoPesquisa) {
return i
}
}
return -1
}
indexOf(arrayIndexOf, 2)
indexOf(arrayIndexOf, "apollo")
includes
function includes(array, element) {
for (let i = 0; i < array.length; i++) {
if(element > 500) {
return true
}else if(element === "duck") {
return true
}
}
return false
}
includes(arrayIncludes, 10)
includes(arrayIncludes, "duck")
some
const callbackSome = (element, i, array) => {
return element > 82 && true;
};
const callbackSome2 = (element, i, array) => {
if (element) {
return false;
}
};
function some(array, callback) {
for (let i = 0; i < array.length; i++) {
if(callback(array[i])){
return true
}
}
return false
}
some(arrySome, callbackSome)
some(arrySome, callbackSome2)
join
function join(array, value=",") {
let res = ""
for (let i = 0; i < array.length; i++) {
if(array[i] === null || array[i] === undefined) {
array[i] = ""
}
if(i !== 0) {
res += value + array[i]
}else {
res += array[i]
}
}
return res
}
join(arrayJoin, " ")
join(arrayJoin, "")
join(arrayJoin, "-")