How to get next and previous five element of from an array?

Rock Havmor

I have an array which consist n element.

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

and also have two button next and prev and initially i am showing the first 5 element(on page load) of an array initial array [1, 2, 3, 4, 5]

how i can show the next five elements on the next button click [6, 7, 8, 9, 10] and on previous button click want to show the [1, 2, 3, 4, 5]

and also need to check the if it does not have any next element if lastIndex is included and if array includes first element.

i have tried using slice to arr.slice(begin[, end])

Gass

const arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
const size = 5
let current = 1

function prev(){
  if(current > 1){
    current--
    let newArr = getNewArr()
    console.log(newArr)
  }
}

function next(){
  if(current >= 1 && current <= (arr.length - 1)/size){
    current++
    let newArr = getNewArr()
    console.log(newArr)
  }
}

function getNewArr(){
  return arr.slice(size*current - size ,size*current)
}
<button onclick=prev()>prev</button>
<button onclick=next()>next</button>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get the next and previous element in an array, Ruby

How to get Previous and Next element of a list in python

How to get previous and next array value in php?

Compare array element previous and next

Grabbing previous and next element in an array

Java list : get next or previous element from an identifier

Get previous and next array by key from array object

How to get the next element in a sparse array?

How to get next element in a sparse array

Get the next element of an array

Get next array element

How to get previous week days and next 2 weeks in array?

How to get previous and next date compared to an array of dates in PHP

Javascript forEach() through an array: how to get the previous and next item?

How to get the next element from monix observable?

How to get the next hash element from hash?

Previous or next element in array based on string "direction"

Next array element must start off with previous

Ember.js previous/next element of an array

How to get a value from previous and next row for each row in BigQuery?

How to get the next/previous item from a spinner on button click?

how to get next and previous posts from wordpress in angular2

How to get the variable from a text box of the previous page to the next page?

How to get next month and previous month from String (Month and Year)

How to get previous, current and next dates from a DataFrame

Get next / previous element using JavaScript?

Get first previous non matched element from array

How to get the index of element in an array and return the next element in Hive?

how to get the max five values from an array in Javascript?