-2

So this little snippet of code takes an array A of integers and makes the integers strings when they become the keys of pairs. Why? Python is happy for them to remain integers, but JavaScript seems to only want strings for keys.

    var pairs = {};
    for(var i=0; i < A.length; ++i) {
        var value = A[i];
        if(pairs[value]) {
            delete pairs[value];
        } else {
            pairs[value] = true;
        }
    }
    console.log(A, pairs)

Produces:

Example test:    [9, 3, 9, 3, 9, 7, 9]
Output:
[ 9, 3, 9, 3, 9, 7, 9 ] { '7': true }
OK

Your test case:  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Output:
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] { '10': true }
Returned value: 10

All the keys are integers when they are added to the pairs object, but they become strings when they are used as keys. This is a good bit different from Python.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
boatcoder
  • 17,525
  • 18
  • 114
  • 178
  • they have to be strings. see this question: https://stackoverflow.com/questions/6066846/keys-in-javascript-objects-can-only-be-strings – zim May 08 '19 at 17:25
  • 3
    See [Keys in Javascript objects can only be strings?](https://stackoverflow.com/questions/6066846/keys-in-javascript-objects-can-only-be-strings) and [the documentation on it](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors) ==> _Property names are Strings or Symbols. Any other value, including a number, is coerced to a string._ – cb64 May 08 '19 at 17:25
  • it's considered a `key` in a object, so, not a integer as in a value. – Anuga May 08 '19 at 17:25
  • https://stackoverflow.com/questions/1450957/pythons-json-module-converts-int-dictionary-keys-to-strings#1450981 – chevybow May 08 '19 at 17:29

3 Answers3

0

By definition, object keys are always strings (or Symbols), if you use something else as a key it will be casted to a string. To have a Map in JS, there is Map, or a Set which will be enough in this case:

  const pairs = new Set();

 for(const value of [1, 2, 1])
   if(!pairs.delete(value)) pairs.add(value);

 console.log([...pairs]);
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

In JavaScript, {} is a literal expression for the Object type. According to the documentation objects map string names to values.

A datatype that accepts arbitrary values as keys is Map.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ctt
  • 1,405
  • 8
  • 18
0

Object keys are always strings. E.g.

let x = {};
x.a = 1;
x[2] = 2;
x[x] = 3;

console.log(x);

On the other hand, if you want integer indices, you can use arrays instead

let A = [9, 3, 9, 3, 9, 7, 9];
var pairs = [];
for (var i = 0; i < A.length; ++i) {
  var value = A[i];
  if (pairs[value]) {
    delete pairs[value];
  } else {
    pairs[value] = true;
  }
}
console.log(A, pairs.reduce((acc, v, i) => {
  if (v) acc.push(i);
  return acc;
}, []));
junvar
  • 11,151
  • 2
  • 30
  • 46