0

In my page, i have no.of check boxes, while load the page, i am collecting some of values and pushing to an array called sendStatus1, again i am assign the value to other variable called submitView,

the reason why i assign, i would like to check, if the both array same or different, if both are different i will go for post else i will not.

but as a dynamic variable, each time the user click on the checkbox, both array are updating, i unable to see any different.

How to find an array is different from while it was loaded status..?

my try:

submitView = sendStatus1; //onload i do.

    var submitStatus = function(){

            var submitButton = $("span.submit").find("button[type='button']");

            console.log(submitView, sendStatus1); //but both are same always..


        }

How can i store the sendStatus1 not updatable on click of checkbox? How to come up with this issue.. any one help me please?

3gwebtrain
  • 14,640
  • 25
  • 121
  • 247
  • on load `submitView = sendStatus1;` and ` console.log(submitView, sendStatus1);` why do you think this should be different...are we missing something ?? – bipen Jul 12 '13 at 07:41
  • on each click of user, i am adding additional data to sendStatus1 so it should not be same right? – 3gwebtrain Jul 12 '13 at 07:42

2 Answers2

1

Because of the way arrays are stored in Javascript if you assign one to another as you do:

submitView = sendStatus1;

you assign the reference of one to the other, and both references then point to the same array.

You need to clone your array which you can do like this:

submitView = sendStatus1.slice(0);

This creates a whole new copy of your array you can use for comparisons.

  • additional info, can you tell me how to compare both are not equal or equal please..? – 3gwebtrain Jul 12 '13 at 07:55
  • Look at this answer [Comparing arrays in javascript](http://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript) –  Jul 12 '13 at 07:59
0

...pushing to an array called sendStatus1, again i am assign the value to other variable called submitView,

When you push something into an array, you modify that array. It doesn't create a new array with the new value in it. So for instance:

var a = [];        // Create an array
var b = a;         // Now both `a` and `b` point to the **same** array
a.push("hi");      // Modify the one array they both point to
console.log(b[0]); // "hi"

We can't help you apply the principle above to your own code because you haven't posted enough of it, but that's likely to be why you're seeing the "two arrays" be the same. It's not two arrays, it's one.

If you really want to create a second array which is a copy of the first, and then add to it, you can do that, but there's usually a better answer.

var a = [];                // Create an array
a.push("First entry");     // Put something in it
var b = [].concat(a);      // Create a *new* array with copies of `a`'s entries
b.push("Second entry");    // Put something on the new array
console.log(a.join(", ")); // "First entry"
console.log(b.join(", ")); // "First entry, Second entry"

(Technically the above is a bit inefficient because there's a temp array created and thrown away. You can also copy an array like this:

var b = [];
b.push.apply(b, a);

...but it's more complicated and I wanted to keep the above simple.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875