Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
function unique (array1, array2) {
var obj1 = {};
for (var i = array1.length-1; i >= 0; -- i)
obj1[array1[i]] = array1[i];
for (var i = array2.length-1; i >= 0; -- i)
obj1[array2[i]] = array2[i];
var result = []
for (var k in obj1) {
result +=(obj1[k]);

}
return result;
}

What I have tried:

I just want to know exactly how it works, when I look at it in the browser debugger it doesn't make much sense.
Posted
Updated 14-Apr-17 9:46am
v3
Comments
Patrice T 14-Apr-17 16:06pm    
Please do not delete the question after solved.
OriginalGriff 14-Apr-17 16:14pm    
Rolled back - otherwise the solution is meaningless ...
Patrice T 14-Apr-17 16:20pm    
++

1 solution

I think that this will ONLY work if array1 and array2 are each arrays of string variables, it won't work for arbitrary array types.

Furthermore, those strings cannot be arbitrary. Specifically they cannot contain white-space (array element "some thing" would not work but "something" would be OK).

I have annotated the code below to explain it. Whoever wrote it should be a little ashamed for not providing some explanatory comments, not least a function header to explain the required argument types. A try-catch block wouldn't have gone amiss either.

Anyway here goes:

JavaScript
function unique (array1, array2) {

   var obj1 = {};  // Declare a new object (see reference below).


    // Loop over array1, working from end to beginning.
    // Take each string element and use this as a key for
    // a to-be-created new property within obj1, that property
    // being assigned the same string value.
    for (var i = array1.length-1; i >= 0; -- i)
      obj1[array1[i]] = array1[i];

    // Similarly loop over array2, copying each element (string
    // value) into obj1 as a property.
    //
    // This is where the uniqueness is achieved because if
    // array2[i] was also contained within array1[<any index>]
    // then this particular string value will already exist
    // within obj1 and the loop below will simply overwrite the
    // same value on top of the existing property.
    for (var i = array2.length-1; i >= 0; -- i)
      obj1[array2[i]] = array2[i];

    // Declare an array to store results.
    var result = []

    // Copy property values from obj1 into the results. These
    // properties are the set of strings which are uniquely listed
    // in array1, array2.
    for (var k in obj1) {
      result +=(obj1[k]);
    }

    return result;
}


The declaration of obj1 follows Javascript best practice for the declaration of a new object.

The key to understanding the above code is the following:

Consider a Javascript object, myObj, containing properties such as "prop1", "prop2" etc. You are probably familiar with accessing those objects using this notation:
JavaScript
var prop1Val = myObj.prop1;
var prop2Val = myObj.prop2;

However, you can also access those properties using this notation:
JavaScript
var prop1Val = myObj["prop1"];
var prop2val = myObj["prop2"];


Hopefully you can now see why I said at the start that array1,2 must only contain simple strings that don't contain white-space. If they did you'd be trying to do something like this:
JavaScript
var prop1Val = myObj["some thing"];

Which as you now know would correspond, in the more familiar notation, to:
JavaScript
var prop1Val = myObj.some thing;  // WHICH IS AN INVALID SYNTAX!


Anyone whoever doubted the beauty of Javascript can't truly know the language. These kind of constructs - although they can be confusing - are exceptionally powerful. It's a language that was well ahead of its time!
 
Share this answer
 
v2
Comments
Member 13127114 14-Apr-17 15:35pm    
Thank you so much! That really helped, I didn't fully understand how objects worked.

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