- it returns a value if the boolean expression is true.
====================
let array = ["apple", "banana", "chery", "draw", "element", "frame", "green", "heaven", "india", "joker"];
function practicingArray(){
let theValue = array.find(function(_, index, _){return index == 3});
document.writeln('key 3 is: ${theValue} ***');
let theKey = array.findIndex(value => value==="green");
document.writeln('green is in key: ${theKey}');
}
====================
HTML:
practicingArray()
Result:
key 3 is: draw *** green is in key: 6
9. findIndex(function(value, index, array){ // boolean statement; })
- it returns the index if the boolean expression is true where counting starts from zero to last index.
10. findLastIndex(function(value, index, array){// boolean statement; })
- it returns the index if the boolean expression is true where counting starts from the last index to zero.
11. filter(function(value, index, array){// boolean statement;})
- it returns an array that makes the boolean expression true.
====================
let array = ["apple", "banana", "chery", "draw", "element", "frame", "green", "heaven", "india", "joker"];
function practicingArray(){
let list1 = array.filter(function(_, index, _){return index < 5});
document.writeln(list1);
}
===================
HTML:
practicingArray()
Result:
apple,banana,chery,draw,element
12. map(function(value, index, array){//statement that you want to return})
- use this function to convert one value to another like converting a string word to its length.
===================
let array = ["apple", "banana", "chery", "draw", "element", "frame", "green", "heaven", "india", "joker"];
function practicingArray(){
let list1 = array.map(function(value, index, _){return '${index}: ${value}';});
document.writeln(list1);
}
===================
HTML
practicingArray()
Result:
0: apple,1: banana,2: chery,3: draw,4: element,5: frame,6: green,7: heaven,8: india,9: joker
13. sort() or sort(function(a, b){//statements})
14. reverse()
15. split(delimiter)
- you can use split if you want to convert a string to an array and you can have an option of a different delimiter rather than just comma.
16. join(delimiter)
- you can use join if you want to convert an array to string and you can have an option of a different delimiter rather than just comma.
17. reduce(function(accumulator, value, index, array){//statement calculation for accumulator}, initial value of accumulator)
- this functions returns a single value which is the accumulator. You can make some computation in the statements to update the accumulator. It iterates starting from index zero to last index.
==================
let array = [100, 200, 300];
function practicingArray(){
let answer = array.reduce(function(accumulator, value, _ , _){
return accumulator + value;
}, 1000);
document.writeln(answer);
}
==================
HTML:
practicingArray()
Result:
1600
Note: The initial value of accumulator is 1000. As it iterates from each element, it adds each element to the accumulator.
18. reduceRight(function(accumulator, value, index, array){//statement calculation for accumulator}, initial value of accumulator)
- this functions returns a single value which is the accumulator. You can make some computation in the statements to update the accumulator. It iterates starting from the last index to zero.
19. Array.isArray(list)
- This functions works like typeof but this is exclusive for array testing. If you want to test a variable if it is an array or not, substitute it as an argument to the parameter list.
20. Most method in Array has an option parameter thisArg except for sort. For example, the function filter(function(value, index, array){// boolean statement}, thisArg)
- thisArg functions as this keyword
==================
let array = [1100, 200, 1300, 1500, 500];
let object1 = {
lowerLimit: 1000,
upperLimit: 2000,
checkIfWithinLimit(arr){
return arr > this.lowerLimit && arr < this.upperLimit;
}
};
function practicingArray(){
let list1 = array.filter(object1.checkIfWithinLimit, object1);
document.writeln(list1);
}
==================
HTML:
practicingArray()
Result:
1100,1300,1500
Note: There are some more functions. You can check them when you are practicing.
YOU ARE READING
Javascript Programming
RandomI am just documenting things what i learned online in here. I do express things in my own words. And I create my own program samples which serves as my practice. I do this so that when someday I would need some notes to review, I can just visit in h...
Useful Array functions
Start from the beginning
