MMMMMCCLXXVII
MMMMMCCLXXVII

Reputation: 591

Delete an array element in Set in Javascript

Take the following example:

var s = new Set([1,2,3]);
s.add([4,5]); //Set(5) {1, 2, 3, 4, [4, 5]}
s.delete([4,5]);//false

I also tried s.delete(Array(4,5)) and s.delete('[4,5]'), but they don't work either.

Why is that? Do I have to consider the implementation code of the delete function?

Upvotes: 2

Views: 2594

Answers (4)

Markus Demarmels
Markus Demarmels

Reputation: 61

Although it is very cumbersome, I think it should work to delete an array element in a set. Is ther a better/ simpler solution?


function isEqual(obj1, obj2) {
   var props1 = Object.getOwnPropertyNames(obj1);
   var props2 = Object.getOwnPropertyNames(obj2); if (props1.length != props2.length) {
      return false;
   } for (var i = 0; i < props1.length; i++) {
      let val1 = obj1[props1[i]];
      let val2 = obj2[props1[i]];
      let isObjects = isObject(val1) && isObject(val2); if (isObjects && !isEqual(val1, val2) || !isObjects && val1 !== val2) {
         return false;
      }
   }
   return true;
}

var s = new Set();
var array = [4, 5];
var array2 = [4, 5];

s.add(1);
s.add(2);
s.add(3);
s.add([4, 5]);


function isObject(obj){

   return typeof obj === "object"

}

for (element of s) {

   console.log('before: ' + element + " with type " + typeof element);

}

array2Filter = function (x) {
   return x == array;
}


for (let element of s) {

   if (typeof element === "object") {

      console.log('element: ' + element + " == object");

      if (isEqual(element, array2)) {

         s.delete(element);

         console.log('test: ' + element + " == " + array2);

      }
      else {

         console.log('test: ' + element + " != " + array2);

      }
   } else {

      console.log('test: ' + element + " != " + array2);

   }
}


console.log('after delete: ' + [...s]);
```   §

Upvotes: 0

Lennholm
Lennholm

Reputation: 7480

The [4,5] you add is a different object than the [4,5] you tell it to delete. Remember, objects are referenced in JS, so you're using two separate references to two different objects that just happens to look the same.

Here's how you use the same object and its reference:

var s = new Set([1,2,3]);
var newItem = [4, 5];
s.add(newItem); //Set(5) {1, 2, 3, 4, [4, 5]}
s.delete(newItem);

It's not the implementation of the delete method you need to consider, as it's completely consistent regardless of what you pass it, you need to consider the fact that in JS object types are always referenced while primitive types are not.

Upvotes: 3

Jonathan.Brink
Jonathan.Brink

Reputation: 25413

This is because you need to pass a reference to the array you want to remove to delete, rather than a separate array that happens to have the same values.

Try this:

var s = new Set([1,2,3]);
var array = [4,5];

s.add(array);

console.log('before: ' + [...s]);

s.delete(array);

console.log('after delete: ' + [...s]);

JSBin: http://jsbin.com/nedoxenevu/1/edit?html,js,console

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386680

You need a reference to the array for deleting the same array. Objects are different if they do not share the same reference.

var s = new Set([1, 2, 3]),
    a = [4, 5];

s.add(a);
console.log([...s]);

s.delete(a);
console.log([...s]);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 2

Related Questions