使用元组或对象进行映射 [英] Map using tuples or objects
问题描述
我正在尝试使用新的(ES6) Map
对象,以表示属性和值之间的映射.
I'm trying to use the new (ES6) Map
objects in order to represent a map between properties and a value.
我的对象的形式类似于:
I have objects in a form similar to:
{key1:value1_1,key2:value2_1},..... {key1:value1_N,key2:value2_N}
我想根据两者的key1 和 key2值对它们进行分组.
I want to group them based on both their key1 and key2 value.
例如,我希望能够按x
和y
将以下内容分组:
For example, I want to be able to group the following by x
and y
:
[{x:3,y:5,z:3},{x:3,y:4,z:4},{x:3,y:4,z:7},{x:3,y:1,z:1},{x:3,y:5,z:4}]
并获取包含以下内容的地图:
And obtain a Map containing:
{x:3,y:5} ==> {x:3,y:5,z:3},{x:3,y:5,z:4}
{x:3,y:4} ==> {x:3,y:4,z:4},{x:3,y:4,z:7}
{x:3,y:1} ==> {x:3,y:1,z:1}
在Python中,我将使用元组作为字典键. ES6映射允许将任意对象用作键,但是使用标准的相等算法(===
),因此根据我的判断,对象仅按引用相等.
In Python, I'd use tuples as dictionary keys. ES6 map allow arbitrary objects as keys but use the standard equality algorithm (===
) so objects are only equal by reference from what I can tell.
如何使用ES6映射完成这种分组?另外,如果我忽略了一种优雅的方法,那么可以使用普通的JS对象作为解决方案.
How can I accomplish this sort of grouping using ES6 maps? Alternatively, a solution using normal JS objects if there is an elegant way I overlooked.
我宁愿不使用外部馆藏库-但如果有一个更好的解决方案,我也有兴趣对其进行研究.
I'd rather not use an external collections library - but if there is a better solution using one I'm interested in learning about it too.
推荐答案
好,我在讨论现在,我从Mozilla的 Jason Orendorff 得到了答案:
Ok, I've raised the issue on esdiscuss now and I got an answer from Mozilla's Jason Orendorff:
- 这 是ES6地图的问题.
- 该解决方案将以ES7 值对象的形式出现,而不是对象
- 之前曾考虑过让人们指定
.equals
和.hashCode
,但是由于支持值对象而被拒绝. (我认为是有充分理由的.) - 到目前为止,唯一的解决方案是滚动自己的收藏集.
- This is a problem with ES6 maps.
- The solution will come in the form of ES7 value objects for keys instead of objects.
- It was considered before to let people specify
.equals
and.hashCode
but it was rejected in favor of value objects. (for good reasons in my opinion). - The only solution as of now is to roll your own collection.
Bradley在ESDiscuss线程上提供了一个基本的此类集合(概念,请勿在生产代码中使用),并且可能看起来像这样:
A basic such collection (concept, don't use in production code) was offered by Bradley on the ESDiscuss thread and might look something like this:
function HashMap(hash) {
var map = new Map;
var _set = map.set;
var _get = map.get;
var _has = map.has;
var _delete = map.delete;
map.set = function (k,v) {
return _set.call(map, hash(k), v);
}
map.get = function (k) {
return _get.call(map, hash(k));
}
map.has = function (k) {
return _has.call(map, hash(k));
}
map.delete = function (k) {
return _delete.call(map, hash(k));
}
return map;
}
function TupleMap() {
return new HashMap(function (tuple) {
var keys = Object.keys(tuple).sort();
return keys.map(function (tupleKey) { // hash based on JSON stringification
return JSON.stringify(tupleKey) + JSON.stringify(tuple[tupleKey]);
}).join('\n');
return hashed;
});
}
一个更好的解决方案是使用 MontageJS/Collections 之类的东西,它允许指定哈希/等于函数.
A better solution is to use something like MontageJS/Collections which allows for specification of hash/equals functions.
您可以在此处上查看API文档.
You can see the API docs here.
这篇关于使用元组或对象进行映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!