Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get next few elements in an array but jump back to the start when passing the last element?

Imagine I have the following simple array:

const myArr = ["el1", "el2", "el3", "el4", "el5", "el6", "el7"];

Now I want to get the next for instance 3 elements after "el5" (index 4). As you can see there are only 2 elements left in the array. When hitting the last index in the array I want to go back to the start and continue there.

This should be the expected output when the start is "el5" (index 4): ["el6", "el7", "el1"].

And this is what I've tried so far.

const myArr = ["el1", "el2", "el3", "el4", "el5", "el6", "el7"];
let output = [];

const followingElementsCount = 3;
let startIndex = myArr.findIndex(el => el === "el5") + 1;
let overflow = 0;
for (let i = 0; i < followingElementsCount; i++) {
  if (startIndex + i >= myArr.length) {
    startIndex = 0;
    overflow++;
  }

  output.push(myArr[startIndex + i + overflow]);
}

console.log(output);

I can't believe it but I'm not capable of solving this probably fairly simple problem.

like image 276
Behemoth Avatar asked Apr 16 '21 18:04

Behemoth


People also ask

What is the correct way to access the last element in an array?

Accessing the last element If we know the number of elements in the array, using index we can find the last element. For instance, if an array named arr consists of 5 elements then the last element is arr[4].

How do I find the last 5 elements of an array?

To get the last N elements of an array, call the slice method on the array, passing in -n as a parameter, e.g. arr. slice(-3) returns a new array containing the last 3 elements of the original array. Copied!

How do you use an array in HTML?

Using an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.


Video Answer


2 Answers

You could adjust with the remainder of the length of the array.

const
    array = ["el1", "el2", "el3", "el4", "el5", "el6", "el7"],
    output = [],
    followingElementsCount = 3,
    index = array.findIndex(el => el === "el5") + 1;

for (let i = 0; i < followingElementsCount; i++) {
    output.push(array[(index + i) % array.length]);
}

console.log(output);

A slightly other approach by using slice

let
    array = ["el1", "el2", "el3", "el4", "el5", "el6", "el7"],
    count = 3,
    index = array.findIndex(el => el === "el5") + 1,
    output = [
        ...array.slice(index, index += count),
        ...(index >= array.length ? array.slice(0, index % array.length) : [])
    ];

console.log(output);

An even shorter approach by using the double length.

let
    array = ["el1", "el2", "el3", "el4", "el5", "el6", "el7"],
    count = 3,
    index = array.findIndex(el => el === "el5") + 1,
    output = [...array, ...array].slice(index, index + count);

console.log(output);
like image 130
Nina Scholz Avatar answered Oct 26 '22 20:10

Nina Scholz


Using modulo:

const myArr = ["el1", "el2", "el3", "el4", "el5", "el6", "el7"];
const followingElementsCount = 3;
const startIndex = myArr.findIndex(el => el === "el5") + 1;

let output = [];
for (let i = 0; i < followingElementsCount; i++) {
  output.push(myArr[(startIndex + i) % myArr.length]);
}

console.log(output);
like image 33
Majed Badawi Avatar answered Oct 26 '22 19:10

Majed Badawi