Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
BlueHost Web Hosting

Bonsai Boy of New York Bonsai Trees and Accessories

Liquid Web iThemes WordPress Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


getResponse autofunnels

Liquid Web Cloud VPS Hosting

Disclosure We are a professional Information website that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

12,195 questions

16,607 answers

573 users

How to copy part of array to another location in the same array without modifying its size in JavaScript

15 Answers

0 votes
var arr = [1, 2, 3, 4, 5];

arr.copyWithin(-1);
for (i = 0; i < arr.length; i++)
  document.write("arr[" + i + "] = " + arr[i] + "<br />");
 

/*
run:  

arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 1

*/

 




answered May 17, 2016 by avibootz
0 votes
var arr = [1, 2, 3, 4, 5];

arr.copyWithin(-2);
for (i = 0; i < arr.length; i++)
  document.write("arr[" + i + "] = " + arr[i] + "<br />");
 

/*
run:  

arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 1
arr[4] = 2

*/

 




answered May 17, 2016 by avibootz
0 votes
var arr = [1, 2, 3, 4, 5];

arr.copyWithin(-3);
for (i = 0; i < arr.length; i++)
  document.write("arr[" + i + "] = " + arr[i] + "<br />");
 

/*
run:  

arr[0] = 1
arr[1] = 2
arr[2] = 1
arr[3] = 2
arr[4] = 3

*/

 




answered May 17, 2016 by avibootz
0 votes
var arr = [1, 2, 3, 4, 5];

arr.copyWithin(-4);
for (i = 0; i < arr.length; i++)
  document.write("arr[" + i + "] = " + arr[i] + "<br />");
 

/*
run:  

arr[0] = 1
arr[1] = 1
arr[2] = 2
arr[3] = 3
arr[4] = 4

*/

 




answered May 17, 2016 by avibootz
0 votes
var arr = [1, 2, 3, 4, 5];

arr.copyWithin(-5);
for (i = 0; i < arr.length; i++)
  document.write("arr[" + i + "] = " + arr[i] + "<br />");
 

/*
run:  

arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5

*/

 




answered May 17, 2016 by avibootz
0 votes
var arr = [1, 2, 3, 4, 5];

arr.copyWithin(-6);
for (i = 0; i < arr.length; i++)
  document.write("arr[" + i + "] = " + arr[i] + "<br />");
 

/*
run:  

arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5

*/

 




answered May 17, 2016 by avibootz
0 votes
var arr = [1, 2, 3, 4, 5];

arr.copyWithin(1);
for (i = 0; i < arr.length; i++)
  document.write("arr[" + i + "] = " + arr[i] + "<br />");
 

/*
run:  

arr[0] = 1
arr[1] = 1
arr[2] = 2
arr[3] = 3
arr[4] = 4

*/

 




answered May 18, 2016 by avibootz
0 votes
var arr = [1, 2, 3, 4, 5];

arr.copyWithin(2);
for (i = 0; i < arr.length; i++)
  document.write("arr[" + i + "] = " + arr[i] + "<br />");
 

/*
run:  

arr[0] = 1
arr[1] = 2
arr[2] = 1
arr[3] = 2
arr[4] = 3

*/

 




answered May 18, 2016 by avibootz
0 votes
var arr = [1, 2, 3, 4, 5];
 
arr.copyWithin(3);
for (i = 0; i < arr.length; i++)
  document.write("arr[" + i + "] = " + arr[i] + "<br />"); 

/*
run:  

arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 1
arr[4] = 2

*/

 




answered May 18, 2016 by avibootz
0 votes
var arr = [1, 2, 3, 4, 5];
 
arr.copyWithin(4);
for (i = 0; i < arr.length; i++)
  document.write("arr[" + i + "] = " + arr[i] + "<br />"); 

/*
run:  

arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 1

*/

 




answered May 18, 2016 by avibootz
0 votes
var arr = [1, 2, 3, 4, 5];
 
arr.copyWithin(0, 1);
for (i = 0; i < arr.length; i++)
  document.write("arr[" + i + "] = " + arr[i] + "<br />"); 

/*
run:  

arr[0] = 2
arr[1] = 3
arr[2] = 4
arr[3] = 5
arr[4] = 5

*/

 




answered May 18, 2016 by avibootz
0 votes
var arr = [1, 2, 3, 4, 5];
 
arr.copyWithin(0, 2);
for (i = 0; i < arr.length; i++)
  document.write("arr[" + i + "] = " + arr[i] + "<br />"); 

/*
run:  

arr[0] = 3
arr[1] = 4
arr[2] = 5
arr[3] = 4
arr[4] = 5

*/

 




answered May 18, 2016 by avibootz
0 votes
var arr = [1, 2, 3, 4, 5];
 
arr.copyWithin(0, 3);
for (i = 0; i < arr.length; i++)
  document.write("arr[" + i + "] = " + arr[i] + "<br />"); 

/*
run:  

arr[0] = 4
arr[1] = 5
arr[2] = 3
arr[3] = 4
arr[4] = 5

*/

 




answered May 18, 2016 by avibootz
...