JavaScript Shift final given number of elemts to front of array

I've got an array

var itemsArr = ["blue", "wine", "orange", "yellow", "cyan"];

How can I get a certain amount of array elements from the END to the FRONT of the array?

Expected output:

example 1:

//shift final 3 elements
var expected = ["orange", "yellow", "cyan", "blue", "wine"];

example 2:

//shift final 2 elements
var expected = ["yellow", "cyan", "blue", "wine", "orange"];
Answer:1

You can use a combination with Array#splice and Array#concat.

var itemsArr = ["blue", "wine", "orange", "yellow", "cyan"];

itemsArr = itemsArr.splice(-3).concat(itemsArr);
document.write('<pre>' + JSON.stringify(itemsArr, 0, 4) + '</pre>');

itemsArr = itemsArr.splice(3).concat(itemsArr);
document.write('<pre>' + JSON.stringify(itemsArr, 0, 4) + '</pre>');
Answer:2

Here is one way to do it:

JSBin Example

var a = ["blue", "wine", "orange", "yellow", "cyan"];

function shifter(arr, n) {
  var shifted = arr.splice(-n);
  return shifted.concat(arr);
}


console.log(shifter(a, 3));
Answer:3

I received Binary Data as String, and how do I convert it to JS Number with little endian CPU? Does ArrayBuffer, DataView useful? It is not just radix converting question. 16 in 2 radix is 10000 not ...

I received Binary Data as String, and how do I convert it to JS Number with little endian CPU? Does ArrayBuffer, DataView useful? It is not just radix converting question. 16 in 2 radix is 10000 not ...

  1. binary number data type
  2. binary number data
  3. data binary number system
  4. binary number data in c
  5. binary number data type sql
  6. binary data to number python
  7. c binary number data type

I make an AJAX call to my server to read a file and get the value back as response. If the request was successful then i replace the innerHTML from my <p> element with id output with the ...

I make an AJAX call to my server to read a file and get the value back as response. If the request was successful then i replace the innerHTML from my <p> element with id output with the ...

  1. detect variable change javascript
  2. detect variable change angular 6
  3. detect variable change angular
  4. detect variable change c#
  5. detect variable change js
  6. detect variable change angular 5
  7. detect variable change python
  8. detect variable change jquery
  9. detect variable change
  10. detect variable change angular 7
  11. detect variable change angular 2
  12. detect variable change angular 8
  13. detect variable change matlab
  14. java detect variable change
  15. ngonchanges detect variable change
  16. unity detect variable change
  17. angular detect variable change in component
  18. angular detect variable change in service
  19. angularjs detect variable change
  20. vue detect variable change

I have modified a simple nfc on-detect, to send a simple byte array over in an separate cordova app using chariotstudios-nfc-plugin . The code is shown below. index.js var app = { // Application ...

I have modified a simple nfc on-detect, to send a simple byte array over in an separate cordova app using chariotstudios-nfc-plugin . The code is shown below. index.js var app = { // Application ...

I have an array arr = [ [1,2,3], [4,5,6], [7,8,9] ] I want to remove the array that contains the element 4. Here it would remove 4,5,6. So it'd be arr = [[1,2,3],[7,8,9]]; I tried a for loop but I ...

I have an array arr = [ [1,2,3], [4,5,6], [7,8,9] ] I want to remove the array that contains the element 4. Here it would remove 4,5,6. So it'd be arr = [[1,2,3],[7,8,9]]; I tried a for loop but I ...

  1. remove certain values from list python
  2. remove certain values in r
  3. remove certain values from a list in r
  4. remove certain values from array matlab
  5. remove certain values from vector r
  6. remove certain values from dictionary python
  7. remove certain values from dataframe
  8. remove certain values from array python
  9. remove certain values from list excel
  10. remove certain values from array js
  11. remove certain values from array vba
  12. remove certain values from data frame r
  13. remove certain values from vector matlab
  14. remove certain values from column in r
  15. remove certain values from excel
  16. remove certain values from array php
  17. remove certain values from numpy array
  18. remove certain values pandas
  19. remove certain values from pivot table
  20. remove certain values from excel column