methods on array in javascript code example
Example 1: javascript array methods
let fruits = ['Apple', 'Banana']
console.log(fruits.length)
Example 2: array methods in javascript
<script>
function getUnique(array){
var uniqueArray = [];
for(var value of array){
if(uniqueArray.indexOf(value) === -1){
uniqueArray.push(value);
}
}
return uniqueArray;
}
var names = ["John", "Peter", "Clark", "Harry", "John", "Alice"];
var uniqueNames = getUnique(names);
console.log(uniqueNames);
</script>
Example 3: functions in arrays javascript
var argsContainer = ['hello', 'you', 'there'];
var functionsContainer = [];
for (var i = 0; i < argsContainer.length; i++) {
var currentArg = argsContainer[i];
functionsContainer.push(function(currentArg){
console.log(currentArg);
});
};
for (var i = 0; i < functionsContainer.length; i++) {
functionsContainer[i](argsContainer[i]);
}