lang/node
javascript: 교집합(Union), 합집합(Intersection), 여집합(difference, minus)
C/H
2021. 1. 21. 11:58
- ECMAScript 6 sets: union, intersection, difference
- [itertools — Functions creating iterators for efficient looping](https://docs.python.org/3/library/itertools.html]
Union
let a = new Set([1,2,3]);
let b = new Set([4,3,2]);
let union = new Set([...a, ...b]);
// {1,2,3,4}
Intersection
let a = new Set([1,2,3]);
let b = new Set([4,3,2]);
let intersection = new Set(
[...a].filter(x => b.has(x)));
// {2,3}
Difference
let a = new Set([1,2,3]);
let b = new Set([4,3,2]);
let difference = new Set(
[...a].filter(x => !b.has(x)));
Choice Key
const o = {
a: 'somestring',
b: 42,
c: 0,
d: 1,
e: 1,
f: 1
};
keys = `a,b,c,d`;
//var obj = Object.fromEntries(Object.entries(o).filter(item=>keys.indexOf(item[0])>=0))
var obj = Object.fromEntries(Object.entries(o).filter(item=>keys.replace(/\s/g,"").split(",").includes(item[0])))
// { a: "somestring", b: 42, c: 0, d: 1 }
반응형