0

I have this Javascript array:

var cars = ["Saab", "Volvo", "BMW", "Volkswagen", "Fiat"];

I know that I can get each of these elements by index like this:

cars[2] // => "BMW"

But how can I get three sequential elements at once?

getThreeCarsByIndex(4) // => ["Fiat", "Saab", "Volvo"]

Thanks for your help.

4
  • 2
    it's not clear. please do explain more. is it circularly linked? Commented Jan 12, 2021 at 7:26
  • use modular arithmetic - 4 % cars.length, (4 + 1) % cars.length, (4 + 2) % cars.length Commented Jan 12, 2021 at 7:31
  • 2
    cars.slice(1,4) Commented Jan 12, 2021 at 7:31
  • youll need to order your array first to get your required values to the start of the array and then use slice Commented Jan 12, 2021 at 7:32

7 Answers 7

2

Just use simple modular arithmetic-

const cars = ["Saab", "Volvo", "BMW", "Volkswagen", "Fiat"];

function getThreeCarsByIndex(ix) {
    return [0, 1, 2].map(offset => cars[(ix + offset) % cars.length]);
}

console.log(getThreeCarsByIndex(4));

When cars.length is 5, 4 % cars.length gives you 4, 5 % cars.length wraps it back to 0 and so on

Oh and also here's a general version because why not-

function getXElementsFrom(start, x, arr) {
    return [...Array(x).keys()].map(offset => arr[(start + offset) % arr.length]);
}

const cars = ["Saab", "Volvo", "BMW", "Volkswagen", "Fiat"];

console.log(getXElementsFrom(4, 3, cars));

[...Array(x).keys()] is basically the equivalent of range(x) in languages like python.

Sign up to request clarification or add additional context in comments.

Comments

2

Use modulo operator to get the desired index without being out of range.

function getThreeCarsByIndex(array, index)
{
  const result = [];
  
  for (let i = 0; i < 3; i++)
  {
    result.push(array[(index + i) % array.length]);
  }
  
  return result;
}

var cars = ["Saab", "Volvo", "BMW", "Volkswagen", "Fiat"];

console.log(getThreeCarsByIndex(cars, 4));

Comments

1

Use slice. If length is not met (3 here), grab the remaining elements from array begin

const getThreeCarsByIndex = (arr, index) => {
  const res = arr.slice(index, index + 3);
  return res.concat(arr.slice(0, 3 - res.length));
};

var cars = ["Saab", "Volvo", "BMW", "Volkswagen", "Fiat"];

console.log(getThreeCarsByIndex(cars, 4))

Comments

1

Here is one approach

const cars = ["Saab", "Volvo", "BMW", "Volkswagen", "Fiat"];

const getThreeCarsByIndex = (index) => cars.slice(index, index + 3).concat(cars.slice(0, (index + 1) % 3))

console.log(getThreeCarsByIndex(4))

Comments

0

You can define a custom function and check whether the given count + given index is greater than length of the array

const getElementsFromIndex = (arr, ind, n) => {
  if(n + ind > arr.length - 1){
    let elementsFromStart = ind + n - arr.length + 1;
    console.log(elementsFromStart)
    return arr.slice(ind).concat(arr.slice(0, elementsFromStart))
  }
  else return arr.slice(ind, ind + n);
}


const arr = [1,2,3,4,5];
console.log(getElementsFromIndex(arr, 4, 3))

Comments

0

var cars = ["Saab", "Volvo", "BMW", "Volkswagen", "Fiat", "Saab", "Volvo"];

cars.slice(4, 7); // => ["Fiat", "Saab", "Volvo"]

2 Comments

you've made your own question here
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
0

It's enough just to write

cars.sort().slice(1,4);

Of course you can create a function that will work for other arrays as well, e.g.

let cars = ["Saab", "Volvo", "BMW", "Volkswagen", "Fiat"];
    
getSequentionalElements = (arr, firstElement, lastElement) => arr.sort().slice(firstElement, lastElement)

let newArray = getSequentionalElements(cars, 1, 4)

console.log(newArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.