0

Can somebody explain how does seen[item] work?

var a = ["a","a","a","b","b","c","D","D","e",6,6,7,8,9,"a",'b','a',"c","","","",""];

function uniq_fast(a) {
    var seen = {};
    var out = [];
    var len = a.length;
    var j = 0;
    for(var i = 0; i < len; i++) {
         var item = a[i];
         if(seen[item] !== 1) {
               seen[item] = 1;
               out[j++] = item;
         }
    }
    return out;
}

var e = uniq_fast(a);
console.log(e);

It returns undefined everytime:

var a = [1,1,1,1,1,2,2,3,4,4,4,4,4,5,"a",'b','a',"c"];
for(var i = 0; i < a.length; i++) {
  var seen = {};
  var item = a[i];
  var x = seen[item];
  console.log(x);
}
F. Vosnim
  • 476
  • 2
  • 8
  • 23
  • `seen[item]` gets the value of the property of object `seen` whose key is the value of the variable `item`. – Pointy Feb 14 '19 at 15:42
  • @Pointy thank you. Unfortunately I can't get what you mean. – F. Vosnim Feb 14 '19 at 15:47
  • 4
    So in your second snippet, you get undefined for everything, since you do not update the seen variable anymore, where in the first example, it gets updated every time a unique value is found: `seen[item] = 1;` There is nothing that says `seen[ x ] = ???` in the second code, so seen stays an empty object. And if you ask for a non-existing property, namely `console.log(seen[item]);`, you get undefined, since the property item does not exist on the seen object. – Shilly Feb 14 '19 at 15:47
  • 1
    Objects have properties. Properties have names. The `[ ]` operator looks up the value of a property, using the value of the expression between `[ ]` as the property name. – Pointy Feb 14 '19 at 15:48
  • 1
    There's no real difference between `seen[item]` and `a[i]` in the very code you posted. – Pointy Feb 14 '19 at 15:49
  • Possible duplicate of [How can I add a key/value pair to a JavaScript object?](https://stackoverflow.com/questions/1168807/how-can-i-add-a-key-value-pair-to-a-javascript-object) and [Checking if a key exists in a JavaScript object?](https://stackoverflow.com/q/1098040/364696) – ShadowRanger Feb 14 '19 at 15:52

2 Answers2

1

I've added comments to your code, you can also check google Developer tools (or other browser equivalent) set breakpoint before for loop and go step by step to see the value of each variable.

var a = ["a","a","a","b","b","c","D","D","e",6,6,7,8,9,"a",'b','a',"c","","","",""];

function uniq_fast(a) {
    var seen = {};
    var out = [];
    var len = a.length;
    var j = 0;
    // if i == 0;
    for(var i = 0; i < len; i++) {
         var item = a[i]; // item is "a" because that's first item in array
         if(seen[item] !== 1) {
               seen[item] = 1; // seen is {"a": 1} it's the same as seen['a'] = 1
               out[j++] = item;
         }
    }
    return out;
}

in your second code:

var a = ["a","a","a","b","b","c","D","D","e",6,6,7,8,9,"a",'b','a',"c","","","",""];
for(var i = 0; i < a.length; i++) {
  var seen = {}; // you have empty object
  var item = a[i]; // item is "a"
  console.log(seen[item]); // this is undefined because seen["a"] is empty,
                           // you never adding anything to seen
}
jcubic
  • 61,973
  • 54
  • 229
  • 402
0

In essence it's not more than

object['a'] = object.a 
Jasper Lichte
  • 500
  • 4
  • 11