0

I have problem with jQuery array.

I want have two different array.

var main_array = []

function create_array(){

   main_array[0] = {id: 1, status: true, number: 10};
   main_array[1] = {id: 1, status: true, number: 16};
   main_array[2] = {id: 1, status: true, number: 20};

}

function change(array, key, number){

   array[key].number = number

}

create_array()

new_array = change(main_array, 0, 20); 

console.log(main_array)

In this situation I add an element in $main_array array and I want change number and create new array, but when I call change function my main_array is changed to.

I don't want change main_array number.

That's what I'm doing wrong

moon
  • 640
  • 6
  • 14
Vykintas
  • 401
  • 1
  • 8
  • 23
  • 4
    Why are you using dollar-signs as prefixes to non-jQuery variables? There is no use of jQuery in your code at all. – Scott Marcus Nov 26 '17 at 18:53
  • 2
    What is test()? – baao Nov 26 '17 at 18:53
  • 1
    do you want a copy of the array and the change the value? – Nina Scholz Nov 26 '17 at 18:54
  • 1
    The array is given by reference. What you need to do in your change function is to create a new array and return the new array: https://stackoverflow.com/questions/3775480/is-there-a-method-to-clone-an-array-in-jquery – Philipp Maurer Nov 26 '17 at 18:55
  • och sorry... test is change(...) – Vykintas Nov 26 '17 at 18:56
  • @PhilippMaurer Technically, all arguments are passed by value in JavaScript. It's just that objects don't pass as objects, they are passed as a memory location reference, which a copy is then made of (by value). – Scott Marcus Nov 26 '17 at 19:01

1 Answers1

1

Arrays are objects and when you pass an object to a function, you are passing a reference to the memory location of the object, not a copy of the object. So, if the receiving function modifies the incoming argument, it will modify the original object.

Because your array contains objects, they too are passed as references to the underlying object memory locations, you will need to create copies of each object in the array. This is done with Object.assign().

Also (as I mentioned in my comment), prefixing identifiers with a dollar sign is generally considered a convention to denote the identifier holds a reference to a JQuery wrapped set object. Since you are not using JQuery anywhere in your code, I would advise you remove them.

var main_array = [];  // <-- This is where the main array will go
var newArray =   [];  // <-- This will be where the copied array goes

function create_array(){

   main_array[0] = {id: 1, status: true, number: 10};
   main_array[1] = {id: 1, status: true, number: 16};
   main_array[2] = {id: 1, status: true, number: 20};
   
   return main_array;
}

function change(array, key, number){
   // Loop thorugh the original array:
   main_array.forEach(function(obj){
    // Make a copy of the objects in the array and put them in the new array
    newArray.push(Object.assign({}, obj));
   });
   newArray[key].number = number
}

main_array = create_array();

change(main_array, 0, 20); 

console.log(main_array, newArray)
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71