-2

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.

Jenz
  • 8,280
  • 7
  • 44
  • 77
  • It's because `arCopy` is still a reference to the original `ar`. – Rory McCrossan Dec 14 '17 at 15:25
  • `var arCopy = ar;` Is not a copy. It's just an assignment. Both arCopy and ar will point to the same element in memory. – Taplar Dec 14 '17 at 15:25
  • No, `slice` works. We can't help you if all you state is "not working" – Dexygen Dec 14 '17 at 15:26
  • ^ https://jsfiddle.net/2uxf5a0d/ – Taplar Dec 14 '17 at 15:27
  • @RoryMcCrossan This is *not* a duplicate, since he's saying `ar.slice()` doesn't work – Dexygen Dec 14 '17 at 15:27
  • 1
    @GeorgeJempty `slice()` works absolutely fine, and this question is worthless without knowing the OPs exact circumstance. However I've reopened the question in the hopes that the OP will update it with more useful information. – Rory McCrossan Dec 14 '17 at 15:34
  • 1
    @Jenz I placed your code sample in an executable snippet. It does not output `red,blue,kk,kk` - but that's because you're not using the array in a valid manner. Are you getting arrays an objects confused? – Rory McCrossan Dec 14 '17 at 15:36
  • @RoryMcCrossan. No the array in this code is not an array of objects. – Jenz Dec 15 '17 at 09:05

2 Answers2

0

this is what your code do

var ar = []; //create empty Array "ar"
ar['color'] = ['red', 'blue']; //adding an array on 'color' parameter
var arCopy = ar.slice(); //cloning(copy) array "ar" on arCopy

arCopy = ar;//make arCopy and ar the same object (remember that Array inherits from object)
ar['color'].push("kk");//adding "kk" to ar (and arCopy because now is the same object)
arCopy['color'].push("kk");//adding "kk" to arCopy (and ar because now is the same object)
console.log(ar); // outputs red,blue,kk,kk

UPDATE

you can use objects as array but u can't use arrays as objects so you cant add an argument on arrays, only values

var ar = {}; 
ar.color = ['red', 'blue'];
var arCopy = JSON.parse(JSON.stringify(ar));
ar['color'].push("kk1");
arCopy['color'].push("kk2");
console.log(ar); // outputs {"color":["red","blue","kk1"]}
console.log(arCopy); // outputs {"color":["red","blue","kk2"]}

slice() definition:

The slice() method returns the selected elements in an array, as a new array object.

The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.

LPZadkiel
  • 561
  • 1
  • 3
  • 16
0

copy the array by using

arCopy = ar['color'].slice();

it will copy the array created inside your array to the respective array

Chandan Sarma
  • 308
  • 2
  • 5
  • 19