Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have an object stored in a var, i push it into an array, i filter through the array and change the some of the values of the object, does the object in the origional var change?

What I have tried:

does filter method make a difference, i have tried using proxies
Posted
Comments
F-ES Sitecore 13-Dec-17 12:02pm    
Why don't you just try it for yourself and see?

1 solution

It will change for object Type
var obj1 = { id: 1, name: 'abc' };
        var obj2 = { id: 2, name: 'xyx' };
        var ary = [];
        ary.push(obj1);
        ary.push(obj2);
        ary[1].id = 3;
        ary[1].name = 'new name';
        alert(obj2.id + ' -- ' +obj2.name); // 3 -- new name
        alert(ary[1].id + ' -- ' + ary[1].name); // 3 -- new name


The values remains same for Primitive Data Types
var a = 1;
      var b = 2;
      var ary = [];
      ary.push(a);
      ary.push(b);
      ary[1] = 3;
      alert(b); // 2
      alert(ary[1]); // 3
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900