| No | Method Name | Return Type | Callback Parameters | Method Parameters |
|---|---|---|---|---|
| 1 | Array Map | array | element, index, arrayInstance | callbackFn |
| 2 | Array Filter | array | element, index, arrayInstance | callbackFn |
| 3 | Array Every | boolean | element, index, arrayInstance | callbackFn |
| 4 | Array Some | boolean | element, index, arrayInstance | callbackFn |
| 5 | Array Sort | array | elementA, elementB | compareFn |
| 6 | Array Reduce | single value | accumulator, currentValue, currentIndex, array | callbackFn, initialValue |
| Parameter | Description |
|---|---|
| element | Current item/value in the array. |
| index | The index (starting from 0) of the current element. |
| arrayInstance | The original array that the method was called on. |
| elementA, elementB | Used in sort() to compare two values. |
| accumulator | (Used in reduce) The accumulated value from previous iterations. |
| currentValue | (Used in reduce) The current element being processed. |
| currentIndex | (Used in reduce) The index of the current element. |
| array | (Used in reduce) The original array being reduced. |
| Parameter | Description |
|---|---|
| callbackFn | The function that runs for each element in the array. |
| compareFn | (Used in sort) Function that defines how two values are compared. |
| initialValue | (Used in reduce) The starting value for the accumulator. |
const arr = [1, 2, 3];
const newArra = [];
for(el of arr){
newArra.push(el);
}
console.log(newArra);Array Map
const newArr = arr.map(function(el){
return el;
})
console.log(newArr);
---
const newArr1 = arr.map((el) => {
return el;
})
console.log(newArr1);const newArr2 = arr.map(el => el * 2);
console.log(newArr2);const characters = [
{
name: 'Luke Skywalker',
height: '172',
mass: '77',
eye_color: 'blue',
gender: 'male',
},
{
name: 'Darth Vader',
height: '202',
mass: '136',
eye_color: 'yellow',
gender: 'male',
},
{
name: 'Leia Organa',
height: '150',
mass: '49',
eye_color: 'brown',
gender: 'female',
},
{
name: 'Anakin Skywalker',
height: '188',
mass: '84',
eye_color: 'blue',
gender: 'male',
},
];const allNames = characters.map(el => el.name);
console.log(allNames);const allHeight = characters.map(el => el.height);
console.log(allHeight);const nameHeight = characters.map(el => {
return{
name: el.name,
height : el.height
}
});
console.log(nameHeight);// const fName = characters.map(el => el.name.split(" ")[0]); // Single Line
const fName = characters.map(el => {
return el.name.split(" ")[0];
});
console.log(fName);Filter Method
const num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const newNum = [];
for (el of num){
if(el % 2 === 0){
newNum.push(el);
}
}
console.log(newNum);const newNum1 = num.filter(el => {
return el % 2 === 0;
})
console.log(newNum1);const characters = [
{
name: 'Luke Skywalker',
height: '172',
mass: '77',
eye_color: 'blue',
gender: 'male',
},
{
name: 'Darth Vader',
height: '202',
mass: '136',
eye_color: 'yellow',
gender: 'male',
},
{
name: 'Leia Organa',
height: '150',
mass: '49',
eye_color: 'brown',
gender: 'female',
},
{
name: 'Anakin Skywalker',
height: '188',
mass: '84',
eye_color: 'blue',
gender: 'male',
},
];const gretherThen100 = characters.filter(el => el.mass > 100);
console.log(gretherThen100);const lessThen200 = characters.filter(function(el){
return el.height < 200;
})
console.log(lessThen200);const maleChar = characters.filter((el) =>{
return el.gender == 'male';
} )
console.log(maleChar);const femaleChar = characters.filter(el => el.gender !== 'male');
console.log(femaleChar);const arr = [2, 4, 6, 8 ];
let isEven;
for(el in arr){
if(el % 2 !== 0){
isEven = true;
}
else{
isEven = false;
}
}
console.log(isEven);const isEven1 = arr.every(el => el % 2 == 0 );
console.log(isEven1);const characters = [
{
name: 'Luke Skywalker',
height: '172',
mass: '77',
eye_color: 'blue',
gender: 'male',
},
{
name: 'Darth Vader',
height: '202',
mass: '136',
eye_color: 'yellow',
gender: 'male',
},
{
name: 'Leia Organa',
height: '150',
mass: '49',
eye_color: 'brown',
gender: 'female',
},
{
name: 'Anakin Skywalker',
height: '188',
mass: '84',
eye_color: 'blue',
gender: 'male',
},
];const checkBlueEyes = characters.every(el => el.eye_color == 'blue');
console.log(checkBlueEyes);const checkMassMoreThen40 = characters.every(el => el.mass > 40);
console.log(checkMassMoreThen40);const shortThen200 = characters.every(el => el.height < 200);
console.log(shortThen200);const isMaleChar = characters.every(el => el.gender == 'male');
console.log(isMaleChar);const oneMaleChar = characters.some(el => el.gender == 'male');
console.log(oneMaleChar);const oneBlueEyes = characters.some(el => el.eye_color == 'blue');
console.log(oneBlueEyes);const oneTaller200 = characters.some(el => el.height > 200);
console.log(oneTaller200);const massLessThen50 = characters.some(el => el.mass < 50);
console.log(massLessThen50);const num = [1, 5, 15, 3, 4, 9, 2, 7];
const sortNum = num.sort();
console.log(sortNum);
const sortNumber = num.sort((a, b) => {
return b - a;
})
console.log(sortNumber); const sortName = characters.sort((a, b) => a.name.localeCompare(b.name));
console.log(sortName);const sortMass = characters.sort((a, b) => a.mass - b.mass);
console.log(sortMass);const sortHeight = characters.sort((a, b) => a.height - b.height);
console.log(sortHeight);const sortGender = characters.sort((a, b) => a.gender.localeCompare(b.gender));
console.log(sortGender);const num = [1, 2, 3, 4, 5, 6, 7];
let result = 0
num.forEach((el) => {
result += el
}, 0)
console.log(result);accumulator, currentValue, currentIndex, array
// accumulator, currentValue, currentIndex, array
const result1 = num.reduce((acc, el) =>{
acc += el;
return acc;
}, 0)
console.log(result1);const characters = [
{
name: 'Luke Skywalker',
height: '172',
mass: '77',
eye_color: 'blue',
gender: 'male',
},
{
name: 'Darth Vader',
height: '202',
mass: '136',
eye_color: 'yellow',
gender: 'male',
},
{
name: 'Leia Organa',
height: '150',
mass: '49',
eye_color: 'brown',
gender: 'female',
},
{
name: 'Anakin Skywalker',
height: '188',
mass: '84',
eye_color: 'blue',
gender: 'male',
},
];const massChar = characters.reduce((acc, el) =>{
acc += Number(el.mass);
return acc;
}, 0);
console.log("mass of all characters: " + massChar);const totalHeight = characters.reduce((acc, el) => {
acc += Number(el.height);
return acc
}, 0)
console.log("height of all characters: " + totalHeight);const allCharName = characters.reduce((acc, el) => {
acc += el.name.split(" ").length;
return acc;
}, 0)
console.log(allCharName);const eyeColorCounter = characters.reduce((acc, el) => {
if(acc[el.eye_color]){
acc[el.eye_color] ++;
}else{
acc[el.eye_color] = 1;
}
return acc;
}, {});
console.log(eyeColorCounter);const str = 'Hello World';
let counter = {};
for(let i = 0; i < str.length; i++ ){
if(str[i] !== ' '){
if(counter[str[i]]){
counter[str[i]] ++;
}
else{
counter[str[i]] = 1;
}
}
};
console.log(counter);let person = {
name : 'Al Muksid',
age : 25,
interest : ['Programming', 'Designing'],
addr : {city: 'Magura', zip: 7600}
}
let {name: fullName = 'Mesion', age, interest, addr : {city, zip}} = person;
console.log(fullName, city);
console.log(`My name is ${fullName}, I am ${age} years old, I am interested in ${interest}, and I live in ${city} with ZIP code ${zip}.`)const name = "Al Muksid";
console.log(name.split(" "));let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
arr.forEach(function (el, ind) { //method
console.log(el, ind);
});Desc:create for each function - Function call as a parameter - then create new prametter(el) inside function- # This function is treated as a method.
arr.forEach((el, ind) => console.log(el, ind)); // # el- element[arr value] # ind- index[index number]