Javascript Sets のまとめ - Javascript Fundamentals - JSchallenger

スポンサーリンク

はじめに

JSchallenger の Javascript Fundamentals – Javascript Sets のまとめです。
まとめページのトップは、JSchallenger まとめ です。

問題一覧と回答

Check if value is present in Set

// Write a function that takes a Set and a value as arguments
// Check if the value is present in the Set

function myFunction(set, val) {
  return set.has(val);
}

Convert a Set to Array

// Write a function that takes a Set as argument
// Convert the Set to an Array
// Return the Array

function myFunction(set) {
  return [...set];
}

Get union of two sets

// Write a function that takes two Sets as arguments
// Create the union of the two sets
// Return the result
// Tipp: try not to switch to Arrays, this would slow down your code

function myFunction(a, b) {
  const result = new Set(a);
  b.forEach((el) => result.add(el));
  return result;
}

Creating Javascript Sets

// Write a function that takes three elements of any type as arguments
// Create a Set from those elements
// Return the result

function myFunction(a, b, c) {
  const set = new Set();
  set.add(a);
  set.add(b);
  set.add(c);
  return set;
}

Delete element from Set

// Write a function that takes a Set and a value as argument
// If existing in the Set, remove the value from the Set
// Return the result

function myFunction(set, val) {
  set.delete(val);
  return set;
}

Add multiple elements to Set

// Write a function that takes a Set and an array as arguments
// If not already existing, add each element in the array to the Set
// Return the modified Set

function myFunction(set, arr) {
  arr.forEach((e) => set.add(e));
  return set;
}

Get Intersection of two Javascript Sets

// Write a function that takes two sets (a and b) as arguments
// Get the intersection of the sets
// In other words, return a set containing all elements that are both in a as well as b

function myFunction(a, b) {
  const int = new Set();
  a.forEach(el => b.has(el) && int.add(el));
  return int;
}

コメント

タイトルとURLをコピーしました