xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

JavaScript convert ES6 Map to Array All In One

JavaScript convert ES6 Map to Array All In One

js convert Map to Array

demos

function differentSymbolsNaive(str) {
  //  write code here.
  const map = new Map();
  const arr = Array.from(str);
  for (const item of arr) {
    if(!map.has(item)) {
      map.set(item, item);
    }
  }
  return [...map].length;
  // return [...map.keys()].length;
}

var majorityElement = function(nums) {
    const map = new Map();
    for(let num of nums) {
      if(!map.has(num)) {
         map.set(num, 1);
      } else {
        map.set(num, map.get(num) + 1);
      }
    }
    const len = Math.floor(nums.length / 3);
    return [...map].filter(arr => arr[1] > len).map(arr => arr[0]);
};

https://leetcode.com/assessment/reports/SW50ZXJ2aWV3U2Vzc2lvbk5vZGU6MjUyMjAwNw==

https://leetcode.com/problems/majority-element-ii/

Array.from

Array.from(arrayLike)
Array.from(arrayLike, mapFn)
Array.from(arrayLike, mapFn, thisArg)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from

Map.values()


const map = new Map().set('a', 1).set('b', 2);
const array = Array.from(map, ([name, value]) => ({ name, value }));
console.log(array);
// [ { "name": "a", "value": 1 }, { "name": "b", "value": 2 } ]


const map = new Map().set('a', 1).set('b', 2);
// 自定义 map 函数
const array = Array.from(map, ([name, value]) => value);
console.log(array);
// [1, 2]

const map = new Map().set('a', 1).set('b', 2);
// _ 约定成俗的私有变量
const array = Array.from(map, ([_, value]) => value);
console.log(array);
// [1, 2]

const map = new Map().set('a', 1).set('b', 2);
// default
const array = Array.from(map.values());
console.log(array);
// [1, 2]


image

Map.keys() & Map.entries()

const map = new Map().set('a', 1).set('b', 2);

map;
// Map(2) {'a' => 1, 'b' => 2}
[...map]
// [Array(2), Array(2)]0: (2) ['a', 1]1: (2) ['b', 2]length: 2[[Prototype]]: Array(0)
[...map.entries()]
// [Array(2), Array(2)]0: (2) ['a', 1]1: (2) ['b', 2]length: 2[[Prototype]]: Array(0)

[...map.values()]
//[1, 2]
[...map.keys()]
// ['a', 'b']

image

demos

var majorityElement = function(nums) {
    const map = new Map();
    for(let num of nums) {
      if(!map.has(num)) {
         map.set(num, 1);
      } else {
        map.set(num, map.get(num) + 1);
      }
    }
    const len = Math.floor(nums.length / 3);
    return [...map].filter(arr => arr[1] > len).map(arr => arr[0]);
};

majorityElement([1,2])
// (2) [1, 2]
majorityElement([1])
// [1]
majorityElement([3,2,3])
// [3]

refs

https://stackoverflow.com/questions/56795743/how-to-convert-map-to-array-of-object

https://stackoverflow.com/questions/35341696/how-to-convert-map-keys-to-array/35341828

https://stackoverflow.com/questions/8763125/get-array-of-objects-keys

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2020-12-23 23:36  xgqfrms  阅读(185)  评论(4编辑  收藏  举报