[JavaScript] how to print odd elements from an array?


I want to print only elements which are in odd indexes, how to do that in Javascript?

1 Answer

3 years ago by

You can use filter function like below to print only the elements with odd indices

  let numbers = ['zero','one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
  console.log(numbers.filter(ele => numbers.indexOf(ele) % 2 !== 0))

Try checking the result here:https://onecompiler.com/javascript/3xmt8zqm4

3 years ago by Meera