Vincent Tang
Vincent Tang

Reputation: 157

ES6 Set comparison and get duplicate

Assume there are 4 ES6 Set for 4 sets of data

state = {
    aSet: new Set().add(1).add(2).add(3).add(4).add(5), // 1,2,3,4,5
    bSet: new Set().add(2).add(3).add(4).add(5).add(6), // 2,3,4,5,6
    cSet: new Set().add(3).add(4).add(5).add(6).add(7), // 3,4,5,6,7
    dSet: new Set().add(4).add(5).add(6).add(7).add(8)  // 4,5,6,7,8
}

Now I want to compare these 4 ES6 Set and get the duplicate (4,5) to form a new Set.

Any suggestion??

Upvotes: 1

Views: 84

Answers (4)

Amardeep Bhowmick
Amardeep Bhowmick

Reputation: 16908

You can use the set intersection principle to do this.

By going over each Set in the state using Array#reduce we compare the old Set from the previous iteration with the new Set in the current iteration and only return the intersection of the two.

The first iteration does no comparison as it takes the first set as the starting point:

const state = {
  aSet: new Set().add(1).add(2).add(3).add(4).add(5), // 1,2,3,4,5
  bSet: new Set().add(2).add(3).add(4).add(5).add(6), // 2,3,4,5,6
  cSet: new Set().add(3).add(4).add(5).add(6).add(7), // 3,4,5,6,7
  dSet: new Set().add(4).add(5).add(6).add(7).add(8) // 4,5,6,7,8
}

const intersection = (sets) => {
  return sets.reduce((prevSet, nextSet, idx) => {
    if (idx === 0) return nextSet;
    return new Set([...nextSet].filter((e) => prevSet.has(e)));
  }, new Set());
}

const sets = Object.values(state);
console.log([...intersection(sets)]);

Upvotes: 1

Ben Stephens
Ben Stephens

Reputation: 3371

Intersection example from: https://2ality.com/2015/01/es6-set-operations.html

const state = {
    aSet: new Set().add(1).add(2).add(3).add(4).add(5), // 1,2,3,4,5
    bSet: new Set().add(2).add(3).add(4).add(5).add(6), // 2,3,4,5,6
    cSet: new Set().add(3).add(4).add(5).add(6).add(7), // 3,4,5,6,7
    dSet: new Set().add(4).add(5).add(6).add(7).add(8)  // 4,5,6,7,8
}

const intersection = (a, b) => new Set([...a].filter(item => b.has(item)));
const intersections = (object) => Object.values(object).reduce(intersection);

console.log(Array.from(intersections(state)).join(', '));

Upvotes: 0

vikash vik
vikash vik

Reputation: 686

const aSet = new Set().add(1).add(2).add(3).add(4).add(5); // 1,2,3,4,5
const bSet = new Set().add(2).add(3).add(4).add(5).add(6); // 2,3,4,5,6
const cSet = new Set().add(3).add(4).add(5).add(6).add(7); // 3,4,5,6,7
const dSet = new Set().add(4).add(5).add(6).add(7).add(8); // 4,5,6,7,8

const allSet = new Set([...aSet, ...bSet, ...cSet, ...dSet]);

const result = [...allSet].reduce((acc, elm) => {
  if (aSet.has(elm) && bSet.has(elm) && cSet.has(elm) && dSet.has(elm))
    acc.add(elm);
  return acc;
}, new Set());

console.log(result);

Upvotes: 1

Thomas Kuhlmann
Thomas Kuhlmann

Reputation: 1003

You can do this:

const state = {
  aSet: new Set().add(1).add(2).add(3).add(4).add(5), // 1,2,3,4,5
  bSet: new Set().add(2).add(3).add(4).add(5).add(6), // 2,3,4,5,6
  cSet: new Set().add(3).add(4).add(5).add(6).add(7), // 3,4,5,6,7
  dSet: new Set().add(4).add(5).add(6).add(7).add(8) // 4,5,6,7,8
}


let intersection = new Set([...state.aSet].filter(x => state.bSet.has(x)&& state.cSet.has(x)&& state.dSet.has(x)))

intersection.forEach(value=>console.log(value))

There's a good article that walks you through the various options you have for comparing sets (intersection, union etc):

Comparing sets

Upvotes: 1

Related Questions