要设置的 JavaScript 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28965112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:39:46  来源:igfitidea点击:

JavaScript Array to Set

javascriptarraysset

提问by Thomas

MSDN references JavaScript's Setcollection abstraction. I've got an array of objects that I'd like to convert to a set so that I am able to remove (.delete()) various elements by name:

MSDN 引用了 JavaScript 的Set集合抽象。我有一个对象数组,我想将它们转换为一个集合,以便我能够.delete()按名称删除 ( ) 各种元素:

var array = [
    {name: "malcom", dogType: "four-legged"},
    {name: "peabody", dogType: "three-legged"},
    {name: "pablo", dogType: "two-legged"}
];

How do I convert this array to a set? More specifically, is it possible to do this without iterating over the above array? The documentation is relatively lacking (sufficient for instantiated sets; not for conversions - if possible).

如何将此数组转换为集合?更具体地说,是否可以在不迭代上述数组的情况下执行此操作?文档相对缺乏(对于实例化集足够了;不适用于转换 - 如果可能的话)。

I may also be thinking of the conversion to a Map, for removal by key. What I am trying to accomplish is an iterable collection that can be accessed or modified via accessing the elements primarily via a key (as opposed to index).

我也可能正在考虑转换为Map,以便按键删除。我想要完成的是一个可迭代的集合,可以通过主要通过键(而不是索引)访问元素来访问或修改它。

Conversion from an array to the other being the ultimate goal.

从一个数组到另一个数组的转换是最终目标。

回答by levi

Just pass the array to the Set constructor. The Set constructor accepts an iterableparameter. The Array object implements the iterableprotocol, so its a valid parameter.

只需将数组传递给 Set 构造函数。Set 构造函数接受一个iterable参数。Array 对象实现了iterable协议,所以它是一个有效的参数。

var arr = [55, 44, 65];
var set = new Set(arr);
console.log(set.size === arr.length);
console.log(set.has(65));

See here

看这里

回答by Ryan Shillington

If you start out with:

如果你开始:

let array = [
    {name: "malcom", dogType: "four-legged"},
    {name: "peabody", dogType: "three-legged"},
    {name: "pablo", dogType: "two-legged"}
];

And you want a set of, say, names, you would do:

而你想要一组,比如说,名字,你会这样做:

let namesSet = new Set(array.map(item => item.name));

回答by AstroCB

What levisaidabout passing it into the constructor is correct, but you could also use an object.

什么利维说,关于它传递到构造是正确的,但你也可以使用一个对象。

I think what Veverkeis trying to say is that you could easily use the deletekeywordon an object to achieve the same effect.

我认为Veverke想说的是,您可以轻松地在对象上使用delete关键字来达到相同的效果。

I think you're confused by the terminology; propertiesare components of the object that you can use as named indices (if you want to think of it that way).

我认为您对术语感到困惑;属性是对象的组件,您可以将其用作命名索引(如果您想这样想的话)。

Try something like this:

尝试这样的事情:

var obj = {
    "bob": "dole",
    "mr.": "peabody",
    "darkwing": "duck"
};

Then, you could just do this:

然后,你可以这样做:

delete obj["bob"];

The structure of the object would then be this:

对象的结构将是这样的:

{
    "mr.": "peabody",
    "darkwing": "duck"
}

Which has the same effect.

这具有相同的效果。

回答by Ashlesh Sortee

By definition "A Set is a collection of values, where each value may occur only once."So, if your array has repeated values then only one value among the repeated values will be added to your Set.

根据定义,“集合是值的集合,其中每个值只能出现一次。” 因此,如果您的数组具有重复值,则重复值中只有一个值会添加到您的 Set 中。

var arr = [1, 2, 3];
var set = new Set(arr);
console.log(set); // {1,2,3}


var arr = [1, 2, 1];
var set = new Set(arr);
console.log(set); // {1,2}

So, do not convert to set if you have repeated values in your array.

因此,如果数组中有重复的值,请不要转换为 set。