Example 1: get index of element in array js
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison'));
console.log(beasts.indexOf('bison', 2));
console.log(beasts.indexOf('giraffe'));
Example 2: js array return only certain positions
const every_nth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
console.log(every_nth([1, 2, 3, 4, 5, 6], 1));
console.log(every_nth([1, 2, 3, 4, 5, 6], 2));
console.log(every_nth([1, 2, 3, 4, 5, 6], 3));
console.log(every_nth([1, 2, 3, 4, 5, 6], 4));
[1,2,3,4,5,6]
[2,4,6]
[3,6]
[4]
Example 3: how to get the index of an array in javascript
search = (arr, item) => { return arr.indexOf(item); }
Example 4: indefOf
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison'));
Example 5: check items in array javascript
function checkAllEven(arr) {
return arr.every(function(x){
return x % 2 === 0
})
}