I'm trying to copy an array ar
which contains string indexes, to another array arCopy
using jquery. The array looks like shown here. Initially I tried by copying arrays like:
var arCopy = ar;
But when I try to add a new value to array arCopy
, it got inserted in ar
also. So I tried with slice
:
var ar = [];
ar['color'] = ['red', 'blue'];
var arCopy = ar.slice();
ar['color'].push("kk");
arCopy['color'].push("kk");
console.log(ar); // outputs red,blue,kk,kk
But slice
is also pushing values to original array ar
. Is this because I'm using array with string index?
Can anyone help me to fix this? Thanks in advance.