Map, Object to Array

steyu·2022년 12월 22일
0

Map to array

const map = new Map();
const [key, value] = Array.from(map)

map.values();
map.keys()

The Array.from method converts the iterable into an array and returns the new array instance.

Object to Array

const [key, value] = Object.entries(map);

valid anagram

const isAnagram = function (s, t) {
  if (s.length !== t.length) {
    return false;
  }
  const sObj = {};
  const tObj = {};

  for (let i = 0; i < s.length; i++) {
    if (!sObj[s[i]]) {
      sObj[s[i]] = 1;
    } else {
      sObj[s[i]]++;
    }
  }
  for (let i = 0; i < t.length; i++) {
    if (!tObj[t[i]]) {
      tObj[t[i]] = 1;
    } else {
      tObj[t[i]]++;
    }
  }

  // compare
  for (const [key, value] of Object.entries(sObj)) {
    if (value !== tObj[key]) {
      return false;
    }
  }
  return true;
};

0개의 댓글