Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying elements in array, and comparing them

Status
Not open for further replies.

dcnguyen

Technical User
Mar 22, 2005
54
US
I'm a little dusty on my pointers and call by reference in JS...

What I want to do is create an array containing a subset of another array. Each entry in the array is an object with several properties.

Does the following work, as far as creating a whole new copy of an element? (I'm not using slice because these elements are not located contiguously in the array)

subArray.push(bigArray[id]);

And let's say I want to delete all elements in bigArray that are contained in subArray? Will this work?

if(subArray == bigArray[j]) {bigArray.splice(j,1);}

I'm guessing it won't since the if statement is comparing memory locations...so I'll have to do something like:

if(subArray.property1 == bigArray[j].property1){bigArray.splice(j,1);}

 
If you use alert(subArray) you'll see the contents of the object as a long string. This means you can compare objects with each other without accessing individual properties.

Lee
 
So the isEqual operator will automatically regard Objects as concatenated strings for the purposes of comparison?
 
Yes. Objects are also associative arrays, which means

subArray.property1

is the same as

subArray['property1']

and allows you to access methods and properties using string variables, like

var prop = 'property1';
subArray[prop];

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top