0
function removeDupes() {
  var out = [],
      obj = {};

   for (x = 0; x < intArray.length; x++) {
    obj[intArray[x]] = 1;
   }
        for (x in obj) {
         out.push(x);
        }
  return out;
}

hi all, obj { } is supposed to have been declared as an object, but why putting [ ] and using obj as array works? thanks

Nate
  • 161
  • 1
  • 1
  • 12
  • Without giving a long winded answer. Check out this site, might help you understanding JS better. It did for me.. https://addyosmani.com/resources/essentialjsdesignpatterns/book/#designpatternsjavascript – Tony May 22 '17 at 14:21
  • What do you mean by “works”? What is the expected result? Why shouldn’t it work? Arrays are also just objects that have key–value pairs. You use arrays for numerical indices. – Sebastian Simon May 22 '17 at 14:21
  • https://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets – baao May 22 '17 at 14:22

2 Answers2

4

someObject["somePropertyName"] is how you access the property of an object. (You can also use someObject.somePropertyName but only if the property name is a valid identifier).

The syntax has nothing specifically to do with arrays.

Arrays are just a type of object with a bunch of methods that treat property names that are integer numbers in special ways.

(Numbers are not valid identifiers so you must use the square bracket syntax to access properties with names that are numbers).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

First of all, let's fix your removedDupes function by adding intArray as an argument and declaring the loop iterators as local variables var x:

function removeDupes(intArray) {
  var out = [],
      obj = {};

  for (var x = 0; x < intArray.length; x++) {
    obj[intArray[x]] = 1;
  }
  for (var x in obj) {
    out.push(x);
  }
  return out;
}

The first loop iterates over all intArray elements. For each element it creates a new property on obj with key intArray[x] and value 1 via obj[intArray[x]] = 1. This is called bracket notation. Now, objects can't have duplicate keys. So adding the same property key twice actually overrides the previously added property. Thus, when the loop completes, your obj has exactly one key per unique array element.

The second loop then pushes these keys into the resulting out array.

There are some issues with your removeDupes function. Firstly, it returns an array of string elements even though the input is an array of number elements. This is because obj keys can't be numbers and are always converted to strings. You can fix it by storing the numeric values along with the keys as follows:

function removeDupes(intArray) {
  var out = [],
      obj = {};

  for (var x = 0; x < intArray.length; x++) {
    obj[intArray[x]] = intArray[x];
  }
  for (var x in obj) {
    out.push(obj[x]);
  }
  return out;
}

Now, this works, but it is very verbose. You can make it shorter by replacing the second loop with return Object.values(obj);. Or even shorter by using a Set:

function removeDupes(intArray) {
  return [...new Set(intArray)];
}
le_m
  • 19,302
  • 9
  • 64
  • 74