|
|
@@ -0,0 +1,200 @@ |
|
|
|
let array = [1,2,3,4]; |
|
|
|
|
|
|
|
//foreach |
|
|
|
array.forEach(function(item){ |
|
|
|
console.log(item); |
|
|
|
}); |
|
|
|
|
|
|
|
Array.prototype.selfForEach = function(callback){ |
|
|
|
for(let i = 0; i < this.length;i++){ |
|
|
|
callback(this[i],i,this); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
array.selfForEach(function(item, index, originalArray){ |
|
|
|
console.log(item); |
|
|
|
console.log(index); |
|
|
|
console.log(originalArray); |
|
|
|
}) |
|
|
|
|
|
|
|
//map |
|
|
|
|
|
|
|
let newArray = array.map(function(item,index,originalArray){ |
|
|
|
return item * index |
|
|
|
}); |
|
|
|
|
|
|
|
Array.prototype.selfMap = function(callback){ |
|
|
|
let ziekeArray = []; |
|
|
|
for(let i = 0; i < this.length; i++){ |
|
|
|
ziekeArray.push(callback(this[i],i,this)); //callback is eigenlijk de uitkomst van een functie. |
|
|
|
|
|
|
|
} |
|
|
|
return ziekeArray; |
|
|
|
} |
|
|
|
|
|
|
|
//filter |
|
|
|
|
|
|
|
let otherArray = [1,2,3,4,5,6]; |
|
|
|
|
|
|
|
let filteredArray = otherArray.filter(function(item,index,originalArray){ |
|
|
|
return item > 2; |
|
|
|
}); |
|
|
|
|
|
|
|
Array.prototype.selfFilter = function(callback){ |
|
|
|
let ziekeArray = []; |
|
|
|
for(let i = 0; i < this.length;i++){ |
|
|
|
if(callback(this[i],i,this)){ //kijken of de conditie van de callback true is |
|
|
|
ziekeArray.push(this[i]); |
|
|
|
} |
|
|
|
} |
|
|
|
return ziekeArray; |
|
|
|
} |
|
|
|
// oef 1 [1,2,3,4,5,6] => [2,4,6] |
|
|
|
let evenArray = otherArray.filter(function(item,index,originalArray){ |
|
|
|
return item % 2 == 0; |
|
|
|
}) |
|
|
|
|
|
|
|
console.log(evenArray); |
|
|
|
|
|
|
|
//oef 2 ['a','b','c'] => ['A','B','C'] |
|
|
|
let letterArray = ['a','b','c']; |
|
|
|
let capitalArray = letterArray.map(function(item,index,originalArray){ |
|
|
|
return item.toUpperCase(); |
|
|
|
}); |
|
|
|
console.log(capitalArray); |
|
|
|
|
|
|
|
//oef 3 [1,2,3] => ['1','2','3'] |
|
|
|
let simpeleArray = [1,2,3]; |
|
|
|
let stringArray = simpeleArray.map(function(item,index,originalArray){ |
|
|
|
return item.toString(); |
|
|
|
}); |
|
|
|
console.log(stringArray); |
|
|
|
//oef4 [ |
|
|
|
// {name: 'tom', age: 5}, |
|
|
|
// {name: 'ben', age: 6}, |
|
|
|
// {name: 'charly', age: 2}, |
|
|
|
// ]; |
|
|
|
// => ['tom','ben','charly'] |
|
|
|
let objectArray = [ |
|
|
|
{name: 'tom', age: 5}, |
|
|
|
{name: 'ben', age: 6}, |
|
|
|
{name: 'charly', age: 2}, |
|
|
|
]; |
|
|
|
let nameArray = objectArray.map(function(item,index,originalArray){ |
|
|
|
return item.name; |
|
|
|
}); |
|
|
|
console.log(nameArray); |
|
|
|
//oef 5 |
|
|
|
// enkel ouder dan 4 |
|
|
|
// {name: 'tom', age: 5}, |
|
|
|
// {name: 'ben', age: 6}, |
|
|
|
// {name: 'charly', age: 2}, |
|
|
|
// ]; |
|
|
|
// => |
|
|
|
// [ |
|
|
|
// {name: 'tom', age: 5}, |
|
|
|
// {name: 'ben', age: 6}, |
|
|
|
//] |
|
|
|
let objectArray2 = [ |
|
|
|
{name: 'tom', age: 5}, |
|
|
|
{name: 'ben', age: 6}, |
|
|
|
{name: 'charly', age: 2}, |
|
|
|
]; |
|
|
|
let nameArray2 = objectArray2.filter(function(item,index,originalArray){ |
|
|
|
return item.age > 4; |
|
|
|
}); |
|
|
|
console.log(nameArray2); |
|
|
|
// oef 6 |
|
|
|
// combinatie van vorige 2 |
|
|
|
// [ |
|
|
|
// { name: 'Tom', age: 5 }, |
|
|
|
// { name: 'Ben', age: 6 }, |
|
|
|
// { name: 'Charly', age: 2 }, |
|
|
|
// ]; |
|
|
|
// => |
|
|
|
//[ 'Tom', 'Ben' ]; |
|
|
|
let nameArray3 = nameArray2.map(function(item,index,originalArray){ |
|
|
|
return item.name; |
|
|
|
}); |
|
|
|
console.log(nameArray3); |
|
|
|
//oef 7 |
|
|
|
// voor plus 18 add naam + 'can go to The Matrix' |
|
|
|
// min 18 add naam + 'is under age!!' |
|
|
|
//[ |
|
|
|
// { |
|
|
|
// name: "Angelina Jolie", |
|
|
|
// age: 80 |
|
|
|
// }, |
|
|
|
// { |
|
|
|
// name: "Eric Jones", |
|
|
|
// age: 2 |
|
|
|
// }, |
|
|
|
// { |
|
|
|
// name: "Paris Hilton", |
|
|
|
// age: 5 |
|
|
|
// }, |
|
|
|
// { |
|
|
|
// name: "Kayne West", |
|
|
|
// age: 16 |
|
|
|
// }, |
|
|
|
// { |
|
|
|
// name: "Bob Ziroll", |
|
|
|
// age: 100 |
|
|
|
// } |
|
|
|
// ]; |
|
|
|
// => |
|
|
|
// [ |
|
|
|
// "Angelina Jolie can go to The Matrix", |
|
|
|
// "Eric Jones is under age!!", |
|
|
|
// "Paris Hilton is under age!!", |
|
|
|
// "Kayne West is under age!!", |
|
|
|
// "Bob Ziroll can go to The Matrix" |
|
|
|
// ] |
|
|
|
let ex7 = [ |
|
|
|
{ |
|
|
|
name: "Angelina Jolie", |
|
|
|
age: 80 |
|
|
|
}, |
|
|
|
{ |
|
|
|
name: "Eric Jones", |
|
|
|
age: 2 |
|
|
|
}, |
|
|
|
{ |
|
|
|
name: "Paris Hilton", |
|
|
|
age: 5 |
|
|
|
}, |
|
|
|
{ |
|
|
|
name: "Kayne West", |
|
|
|
age: 16 |
|
|
|
}, |
|
|
|
{ |
|
|
|
name: "Bob Ziroll", |
|
|
|
age: 100 |
|
|
|
} |
|
|
|
].map(function(item){ |
|
|
|
if(item.age >= 18){ |
|
|
|
return item.name + ' can go to the matrix'; |
|
|
|
} |
|
|
|
return item.name + ' is under age!!!'; |
|
|
|
}); |
|
|
|
|
|
|
|
console.log(ex7); |
|
|
|
|
|
|
|
|
|
|
|
//reduce |
|
|
|
|
|
|
|
let reduceArray = [1,2,3,4]; |
|
|
|
|
|
|
|
let sumOfArray = reduceArray.reduce(function(result,item){ |
|
|
|
return result + item; |
|
|
|
},0); |
|
|
|
|
|
|
|
//oef 8 join van stringarray |
|
|
|
|
|
|
|
let ex8 = ['ik','ben','tof'].reduce(function(result,item){ |
|
|
|
if(result.length === 0){ |
|
|
|
return item; |
|
|
|
} |
|
|
|
return result + item; |
|
|
|
},""); |
|
|
|
console.log(ex8); |