I want to write a program to get nth element of given array in javascript. Using javascript methods. Also do not want to use slice. Please guide me on this.
I have tried this
const array = [1, 2, 3, 5]
function filterItems(array, index) {
return array[index];
}
console.log(filterItems(array, 0));
this will output 1. but how to do using javascript methods and without using slice


Jignesh KumarRank #91 · 22.9K reputationPosted Jan 5, 2023, 3:17 AM EST
Hello Aniket,
You can create function and use that function as below,
const getColumnCells = (cells, columnIndex) => { const columnCells = []; const delta = Math.floor(cells.length / columnIndex); for (let i = columnIndex; i < cells.length; i = i + delta) { columnCells.push(cells[i]); } console.log(columnCells); return columnCells; }; const array = [1, 2, 3, 5] getColumnCells(array, 2)Above one will give Out put : 3
Vishal YelveRank #178 · 13.6K reputationPosted Jan 5, 2023, 6:16 AM EST
function nthElementFinder(a, n) { const result = []; let i = n - 1; while (i < a.length) { result.push(a[i]); i += n; } return result; } console.log(nthElementFinder([1, 2, 3, 5], 3));Aravind GovindarajRank #430 · 5.8K reputationPosted Jan 5, 2023, 7:45 AM EST
try { const array = [1, 2, 3, 5] function filterItems(array, index) { return array[(array.length - (array.length- index))]; } alert(filterItems(array, 3)); } catch(err) { document.getElementById("demo").innerHTML = err.message; }Uday DodiyaRank #239 · 10.3K reputationPosted Jan 6, 2023, 12:36 AM EST
Refer the @Mahesh Chand sir latest article about How to find nth element of Array using Javascript methods?
https://www.c-sharpcorner.com/article/how-to-find-nth-element-of-array-using-javascript-methods/