lang/node

javascript: 교집합(Union), 합집합(Intersection), 여집합(difference, minus)

C/H 2021. 1. 21. 11:58

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 }
반응형

'lang > node' 카테고리의 다른 글

Build a Website Accessibility Tester With JavaScript & Pa11y  (0) 2021.11.10
Joi Validation  (0) 2021.02.10
javascript: 교집합(Union), 합집합(Intersection), 여집합(difference, minus)  (0) 2021.01.21
linux watcher limit on pm2  (0) 2019.11.21
pm2 npm start  (0) 2019.11.14
NPM Permission Error  (0) 2019.11.13