32

Very simple thing I am trying to do in JS (assign the values of one array to another), but somehow the array bar's value doesn't seem affected at all.

The first thing I tried, of course, was simply bar = ar; -- didn't work, so I tried manually looping through... still doesn't work.

I don't grok the quirks of Javascript! Please help!!


var ar=["apple","banana","canaple"];
var bar;

for(i=0;i<ar.length;i++){
    bar[i]=ar[i];
}
alert(ar[1]);

And, here is the fiddle: http://jsfiddle.net/vGycZ/


(The above is a simplification. The actual array is multidimensional.)

ina
  • 19,167
  • 39
  • 122
  • 201
  • check this : http://www.hardcode.nl/subcategory_1/article_414-copy-or-clone-javascript-array-object – Pranay Rana Aug 09 '10 at 07:25
  • You could have spotted the syntax error ("arr" vs "ar") that was pointed out to you by using the Firebug plugin for Firefox. I'd go as far as to say you can't properly do Javascript development without it. – Niels Bom Aug 09 '10 at 07:27
  • i don't do much javascript - and tbh i never figured out how to use Firebug :( ... point a few good tutorials at me? – ina Aug 09 '10 at 08:02

8 Answers8

70

Your code isn't working because you are not initializing bar:

var bar = [];

You also forgot to declare your i variable, which can be problematic, for example if the code is inside a function, i will end up being a global variable (always use var :).

But, you can avoid the loop, simply by using the slice method to create a copy of your first array:

var arr = ["apple","banana","canaple"];
var bar = arr.slice();
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
9

copy-or-clone-javascript-array-object

var a = [ 'apple', 'orange', 'grape' ];
 b = a.slice(0);
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
8

In ES6 you can use Array.from:

var ar = ["apple","banana","canaple"];
var bar = Array.from(ar);
alert(bar[1]); // alerts 'banana'
Luis Cabrera Benito
  • 1,558
  • 13
  • 21
4

You have two problems:

First you need to initialize bar as an array:

var bar = [];

Then arr should be ar in here:

for(i=0;i<arr.length;i++){

Then you'll get alerted your banana :)

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
  • yup - exactly it. too used to php sloppiness, forgot to declare the array! thanks for being the first to answer! – ina Aug 09 '10 at 08:01
4

With ES6+ you can simply do this

const original = [1, 2, 3];
const newArray = [...original];

The documentation for spread syntax is here

To check, run this small code on dev console

const k = [1, 2];
const l = k
k === l
> true
const m = [...k]
m
> [1, 2]
k === m
> false
Arghya C
  • 9,805
  • 2
  • 47
  • 66
2

You have misspelled variable ar Try this

for(i=0;i<ar.length;i++){
    bar[i]=ar[i];
}
alert(ar[1]);
Codler
  • 10,951
  • 6
  • 52
  • 65
  • yes ty for the pointer - this was not actual code, just brutal simplification typed up too quickly for a jsfiddle – ina Aug 09 '10 at 07:25
2

The problem probably here in the for loop statement:

for(i=0;i<ar.length;i++){
    bar[i]=ar[i];
}
alert(ar[1]);

You need to fix to ar.length instead of arr.length. And it's better to initialize bar as: var bar = [].

Artem Barger
  • 40,769
  • 9
  • 59
  • 81
2

In your code, the variable arr in the for loop is not the same as your original array ar: you have one too many r.

slebetman
  • 109,858
  • 19
  • 140
  • 171