Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 可变数组作为ES6映射中的值_Javascript_Arrays_Ecmascript 6 - Fatal编程技术网

Javascript 可变数组作为ES6映射中的值

Javascript 可变数组作为ES6映射中的值,javascript,arrays,ecmascript-6,Javascript,Arrays,Ecmascript 6,在ES6映射中使用数组(可能经常被推送和拼接)作为值有意义吗,还是应该使用普通关联数组 例如,下面的节点服务器代码将跟踪哪些用户已经下载了特定的文件 // initialisation var map = new Map(); map.set ("foo.png",[]); // push a new user id to array associated with hashString function userHasFile(filename,userId) { // retr

在ES6映射中使用数组(可能经常被推送和拼接)作为值有意义吗,还是应该使用普通关联数组

例如,下面的节点服务器代码将跟踪哪些用户已经下载了特定的文件

// initialisation
var map = new Map();
map.set ("foo.png",[]);

// push a new user id to array associated with hashString 
function userHasFile(filename,userId) {

    // retrieve array of all userIds that have downloaded the file
    var users = map.get(filename);

    // delete the map reference since we are going to update it
    map.delete(filename);

    // push the new userId to the array of users that have downloaded file
    users.push(userId);

    // update the Map key to the new array
    map.set(filename,users);
}

// example usage
userHasFile("foo.png","user1");
userHasFile("foo.png","user2");
我是ES6新手,Map API的简单性吸引了我,因此我想知道与使用标准关联阵列相比,这种方法是否有任何好处/缺点(如性能、内存、GC等)

当然

拥有密钥列表存储是一种非常有用的模式,它是其中之一

在地图中保留一个数组是一件完全合法且有用的事情。这是存储符合某些条件的对象列表的好方法,在JavaScript(以及使用引用的其他语言)中尤其有效。映射实际上并不包含数组的副本,只是对数组的引用,因此您可以从映射外部修改数组,映射将在不知道或调整大小的情况下更新

但是,如果功能模式有意义的话,您应该小心使用它们。如果您有传入项,您可能希望将它们流化到map/reduce(使用reduce按键对它们进行分组),而不是保留一个映射并修改底层数组。在这里,您依赖的是参考资料和副作用

如果要防止在某个时刻更新数组,可以使用类似于的库将其替换为不可变数组

如果您在服务器中工作,比如说,需要记录传入请求,但不知道它们何时到达(statsd所做的缓存和刷新类型),则映射是有意义的:

let responses = new Map();

server.on('request', function (url, responseTime) {
  if (!responses.has(url)) {
    responses.set(url, []);
  }

  const times = responses.get(url);
  times.push(responseTime);
});

const flushTimer = setInterval(function () {
  stats.write(responses);
  responses = new Map();
}, 10*1000);
当然

拥有密钥列表存储是一种非常有用的模式,它是其中之一

在地图中保留一个数组是一件完全合法且有用的事情。这是存储符合某些条件的对象列表的好方法,在JavaScript(以及使用引用的其他语言)中尤其有效。映射实际上并不包含数组的副本,只是对数组的引用,因此您可以从映射外部修改数组,映射将在不知道或调整大小的情况下更新

但是,如果功能模式有意义的话,您应该小心使用它们。如果您有传入项,您可能希望将它们流化到map/reduce(使用reduce按键对它们进行分组),而不是保留一个映射并修改底层数组。在这里,您依赖的是参考资料和副作用

如果要防止在某个时刻更新数组,可以使用类似于的库将其替换为不可变数组

如果您在服务器中工作,比如说,需要记录传入请求,但不知道它们何时到达(statsd所做的缓存和刷新类型),则映射是有意义的:

let responses = new Map();

server.on('request', function (url, responseTime) {
  if (!responses.has(url)) {
    responses.set(url, []);
  }

  const times = responses.get(url);
  times.push(responseTime);
});

const flushTimer = setInterval(function () {
  stats.write(responses);
  responses = new Map();
}, 10*1000);