[ES6] When should use Map instead of Object

Use Maps when keys are unknown until runtime:

Map:

let recentPosts = new Map();

createPost( newPost, (data)=>{

    // Key unknown until runtime, so use Map
   recentPosts.set(data.author, data.message);
});

Object:

const POST_PRE_PAGE=15;

// Keys are previously defined, so use object!
let userSettings = {
   perPage: POST_PRE_PAGE,
   showRead: true
}

Use Map when types are the same:

Map:

let recentPosts = new Map();

createPost(newPost, (data) => {
   recentPost.set(data.author, data.message);
});

// ...somehwere else in the code
socket.on(‘new post‘, function(data){
   recentPosts.set(data.author, data.message)
})

Notice in the code, both places keys: ‘data.author‘ are the same type and data: ‘data.message‘ are also the same type.

Object:

const POST_PRE_PAGE = 15;

let userSettings = {
   prePage: POST_PRE_PAGE,
   showRead: true
};
// Some values are numeric, others are boolean, so use Object!
时间: 2024-10-12 10:54:58

[ES6] When should use Map instead of Object的相关文章

es6的Set和Map数据结构

Set 和 Map 数据结构 Set WeakSet Map WeakMap Set § ? 基本用法 ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. const s = new Set(); [2, 3, 5, 4, 5, 2, 2].forEach(x => s.add(x)); for (let i of s) { console.log(i); } // 2 3 5 4 上面代码通过add

[Ramda] Declaratively Map Predicates to Object Properties Using Ramda where

Sometimes you need to filter an array of objects or perform other conditional logic based on a combination of factors. Ramda's where function gives you a concise way to declaratively map individual predicates to object properties, that when combined,

JSON 字符串 转Map<String,List<Object o>>

前提:使用阿里巴巴的JSON包 String json = ""; //JSON字符串 Map map = JSON.parseObject(json, Map.class); //遍历 Map  使用迭代器 Iterator it = map.keySet().iterator(); while(it.hashNext()){ String values = map.get(it.next()).toString(); List<Object o> list = JSON

list中依据map&amp;lt;String,Object&amp;gt;的某个值排序

private void sort(List<Map<String, Object>> list) { Collections.sort(list, new Comparator<Map<String, Object>>(){ public int compare(Map<String, Object> o1, Map<String, Object> o2) { //依据map中某个字段,从小到大排序 return (Integer)

es6 的Set和Map

//Set 和Map类似数组 成员唯一(无重复值) set 构造函数 var arr3=[1,2,3,3,4,5,3,5] a,三种添加方式 const arr4=new Set(); // 1. const arr4=new Set().add(1).add(2).add(3); // 2. add() arr4.add(1) arr4.add(2) arr4.add(3) arr4.add(4) //3. const arr5=new Set([1,2,3,4]) //成员的总数size 属

利用Map创建Copy Object

定义上下文行为,要继承Map package com.wjz.core; import java.io.Serializable; import java.util.Map; public interface CustomSubjectContext extends Map<String, Object> { void setSessionId(Serializable sessionId); Serializable getSessionId(); void setAuthenticated

es6入门set和map

ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set函数可以接受一个数组(或类似数组的对象)作为参数,用来初始化. var set = new Set([1, 2, 3, 4, 4]); [...set]console.log(set) // [1, 2, 3, 4] var s = new Set(); [2, 3, 5, 4, 5, 2, 2].map(x => s.add(x)); console.log(s) // 2 3 5 4 在Set内部,两

【ES6】Set和Map数据结构

1.set 类似于数据,但是成员的值都是唯一的,没有重复的值 const s=new Set() let arr=[1,2,3,4] arr.forEach(x=>s.add(x)) for(let i of s){ console.log(i) } const s=new Set([1,2,3,4,5]) [...s] s.size //5 去除数组重复元素 (Array.from可以将Set解构转为数组) [...new Set(array)]    Array.from(set) 去除字符

使用es6的set和map实现数组去重复

var set = new Set();var arr = [1, 2, 3, 3, 2, 1];arr.map(val => set.add(val));// arr.map(function(val) {// set.add(val);// })arr = [];for(i of set) { arr.push(i);}console.log(arr) // [1, 2, 3] function dedupe(array) { return Array.from(new Set(array)