📜  JavaScript 中的集合

📅  最后修改于: 2022-05-13 01:58:10.110000             🧑  作者: Mango

JavaScript 中的集合

在本文中,我们将讨论 ES6 提供的 Set 对象。集合是唯一的项目的集合,即没有元素可以重复。 ES6 中的集合是有序的:集合的元素可以按插入顺序进行迭代。 Set 可以存储任何类型的值,无论是原始值还是对象。
句法:

new Set([it]);

Parameter: 
it - It is an iterable object whose all elements are 
added to the new set created, 
If the parameter is not specified or null is passed 
then a new set created is empty.

Returns:
A new set object

现在让我们创建一些集合:
例子:

Javascript
// it contains
// ["sumit","amit","anil","anish"]
var set1 = new Set(["sumit","sumit","amit","anil","anish"]);
 
// it contains 'f', 'o', 'd'
var set2 = new Set("fooooooood");
 
// it contains [10, 20, 30, 40]
var set3 = new Set([10, 20, 30, 30, 40, 40]);
 
 // it is an  empty set
var set4 = new Set();


Javascript
// using Set.prototype.add(value)
// creating an empty set
var set1 = new Set();
 
// set contains 10, 20
set1.add(10);
set1.add(20);
 
// As this method returns
// the set object hence chaining
// of add method can be done.
set1.add(30).add(40).add(50);
 
// prints 10, 20, 30, 40, 50
console.log(set1);


Javascript
// using Set.prototype.delete(value)
// creating set it contains
// f, o , d, i, e
var set1 = new Set("foooodiiiieee");
 
// deleting e from the set
// it prints true
console.log(set1.delete('e'));
 
// set contains f, o, d, i
console.log(set1);
 
// deleting an element which is
// not in the set
// prints false
console.log(set1.delete('g'));


Javascript
// Using Set.prototype.clear()
// creating a set
var set2 = new Set([10, 20, 30, 40, 50]);
 
// prints {10, 20, 30, 40, 50}
console.log(set2);
 
// clearing set2
set2.clear()
 
// prints {}
console.log(set2);


Javascript
// Using Set.prototype.entries()
// creating set
var set1 = new Set();
 
// adding element to the set
set1.add(50);
set1.add(30);
set1.add(40);
set1.add(20);
set1.add(10);
 
// using entries to get iterator
var getEntriesArry = set1.entries();
 
// each iterator is array of [value, value]
// prints [50, 50]
console.log(getEntriesArry.next().value);
 
// prints [30, 30]
console.log(getEntriesArry.next().value);
 
// prints [40, 40]
console.log(getEntriesArry.next().value);


Javascript
// Using Set.prototype.has()
// creating set
var set1 = new Set();
 
// adding element to the set
set1.add(50);
set1.add(30);
             
// prints true
console.log(set1.has(50));
 
// prints false
console.log(set1.has(10));


Javascript
// Using Set.prototype.values()
// Using Set.prototype.keys()
// creating set
var set1 = new Set();
 
// adding element to the set
set1.add(50);
set1.add(30);
set1.add(40);
set1.add("Geeks");
set1.add("GFG");
 
// getting all the values
var getValues = set1.values();
 
// prints a SetIterator
// that contains {50, 30, 40, "Geeks", "GFG"}
console.log(getValues);
 
// getting all the values
var getKeys = set1.keys();
 
// prints a SetIterator
// that contains {50, 30, 40, "Geeks", "GFG"}
console.log(getKeys);


Javascript
// using Set.prototype[@@Iterator]()
var set1 = new Set(["sumit","sumit","amit","anish"]);
 
var getit = set1[Symbol.iterator]();
 
// Printing the values in the
// iterator "getit"
 
// prints {value: "sumit", done: false}
console.log(getit.next());
 
// prints {value: "amit", done: false}
console.log(getit.next());
 
// prints {value: "anish", done: false}
console.log(getit.next());
 
// prints {value: undefined, done: true}
console.log(getit.next());


Javascript
// check whether the set on which the
// method is invoked is the subset of
// otherset or not
Set.prototype.subSet = function(otherSet)
{
    // if size of this set is greater
    // than otherSet then it can'nt be
    //  a subset
    if(this.size > otherSet.size)
        return false;
    else
    {
        for(var elem of this)
        {
            // if any of the element of
            // this is not present in the
            // otherset then return false
            if(!otherSet.has(elem))
                return false;
        }
        return true;
    }
}
 
// using the subSet function
 
// Declaring different sets
var setA = new Set([10, 20, 30]);
var setB = new Set([50, 60, 10, 20, 30, 40]);
var setC = new Set([10, 30, 40, 50]);
 
// prints true
console.log(setA.subSet(setB));
 
// prints false
console.log(setA.subSet(setC));
 
// prints true
console.log(setC.subSet(setB));


Javascript
// Perform union operation between
// called set and otherSet
Set.prototype.union = function(otherSet)
{
    // creating new set to store union
    var unionSet = new Set();
 
    // iterate over the values and add
    // it to unionSet
    for (var elem of this)
    {
        unionSet.add(elem);
    }
 
    // iterate over the values and add it to
    // the unionSet
    for(var elem of otherSet)
        unionSet.add(elem);
 
    // return the values of unionSet
    return unionSet;
}
 
// using the union function
// Declaring values for set1 and set2
var set1 = new Set([10, 20, 30, 40, 50]);
var set2 = new Set([40, 50, 60, 70, 80]); 
 
// performing union operation
// and storing the resultant set in
// unionSet
var unionSet = set1.union(set2);
 
// prints [10, 20, 30, 40, 50, 60, 70, 80]
console.log(unionSet.values());


Javascript
// Performs intersection operation between
// called set and otherSet
Set.prototype.intersection = function(otherSet)
{
    // creating new set to store intersection
    var intersectionSet = new Set();
 
    // Iterate over the values
    for(var elem of otherSet)
    {
        // if the other set contains a
        // similar value as of value[i]
        // then add it to intersectionSet
        if(this.has(elem))
            intersectionSet.add(elem);
    }
 
// return values of intersectionSet
return intersectionSet;               
}
// using intersection function
// Declaring values for set1 and set2
var set1 = new Set([10, 20, 30, 40, 50]);
var set2 = new Set([40, 50, 60, 70, 80]); 
 
// performing union operation
// and storing the resultant set in
// intersectionset
var intersectionSet = set1.intersection(set2);
 
// prints {40, 50}
console.log(intersectionSet.values());


Javascript
// Performs difference operation between
// called set and otherSet
Set.prototype.difference = function(otherSet)
{
    // creating new set to store difference
     var differenceSet = new Set();
 
    // iterate over the values
    for(var elem of this)
    {
        // if the value[i] is not present
        // in otherSet add to the differenceSet
        if(!otherSet.has(elem))
            differenceSet.add(elem);
    }
 
    // returns values of differenceSet
    return differenceSet;
}
 
// using difference function
// Declaring values for set1 and set2
var set1 = new Set([10, 20, 30, 40, 50]);
var set2 = new Set([40, 50, 60, 70, 80]); 
 
// performing union operation
// and storing the resultant set in
// intersectionset
var differenceSet = set1.difference(set2);
 
// prints {10, 20, 30}
console.log(differenceSet);


特性:
Set.prototype.size – 它返回Set中元素的数量。
方法:

  • Set.prototype.add() – 它在 Set 对象的末尾添加具有指定的新元素。
    句法:
set1.add(val);

Parameter:
val - It is a value to be added to the set.

Returns: 
The set object

例子:

Javascript

// using Set.prototype.add(value)
// creating an empty set
var set1 = new Set();
 
// set contains 10, 20
set1.add(10);
set1.add(20);
 
// As this method returns
// the set object hence chaining
// of add method can be done.
set1.add(30).add(40).add(50);
 
// prints 10, 20, 30, 40, 50
console.log(set1);
  • Set.prototype.delete() – 它从 Set 对象中删除具有指定的元素。
    句法:
set1.delete(val);

Parameter:
val - It is a value to be deleted from the set.

Returns: 
true if the value is successfully deleted from the set else returns false.

例子:

Javascript

// using Set.prototype.delete(value)
// creating set it contains
// f, o , d, i, e
var set1 = new Set("foooodiiiieee");
 
// deleting e from the set
// it prints true
console.log(set1.delete('e'));
 
// set contains f, o, d, i
console.log(set1);
 
// deleting an element which is
// not in the set
// prints false
console.log(set1.delete('g'));
  • Set.prototype.clear() - 它从集合中删除所有元素。
    句法:
set1.clear();

Parameter:
No parameters

Returns: 
undefined

例子:

Javascript

// Using Set.prototype.clear()
// creating a set
var set2 = new Set([10, 20, 30, 40, 50]);
 
// prints {10, 20, 30, 40, 50}
console.log(set2);
 
// clearing set2
set2.clear()
 
// prints {}
console.log(set2);
  • Set.prototype.entries() – 它返回一个迭代器对象,该对象包含一个数组,该数组按插入顺序包含集合的条目。
    句法:
set1.entries();

Parameter:
No parameters

Returns: 
It returns an iterator object that contains an
array of [value, value] for every 
element of the set, in the insertion order. 

例子

Javascript

// Using Set.prototype.entries()
// creating set
var set1 = new Set();
 
// adding element to the set
set1.add(50);
set1.add(30);
set1.add(40);
set1.add(20);
set1.add(10);
 
// using entries to get iterator
var getEntriesArry = set1.entries();
 
// each iterator is array of [value, value]
// prints [50, 50]
console.log(getEntriesArry.next().value);
 
// prints [30, 30]
console.log(getEntriesArry.next().value);
 
// prints [40, 40]
console.log(getEntriesArry.next().value);
  • Set.prototype.has() - 如果指定的存在于 Set 对象中,则返回 true。
    句法:
set1.has(val);

Parameter:
val - The value to be searched in the Set

Returns: 
True if the value is present else it returns false.

例子:

Javascript

// Using Set.prototype.has()
// creating set
var set1 = new Set();
 
// adding element to the set
set1.add(50);
set1.add(30);
             
// prints true
console.log(set1.has(50));
 
// prints false
console.log(set1.has(10));
  • Set.prototype.values() - 它以相同的插入顺序返回 Set 中的所有值。
    句法:
set1.values();

Parameter:
No parameters

Returns: 
An iterator object that contains all the values of the set in the same order 
as they are inserted. 
  • Set.prototype.keys() - 它还以插入顺序返回 Set 中的所有值。
    注意: -它类似于 Sets 中的values()
    句法:
set1.keys();

Parameter:
No parameters

Returns: 
An iterator object that contains all the 
values of the set in the same order
as they are inserted. 

例子:

Javascript

// Using Set.prototype.values()
// Using Set.prototype.keys()
// creating set
var set1 = new Set();
 
// adding element to the set
set1.add(50);
set1.add(30);
set1.add(40);
set1.add("Geeks");
set1.add("GFG");
 
// getting all the values
var getValues = set1.values();
 
// prints a SetIterator
// that contains {50, 30, 40, "Geeks", "GFG"}
console.log(getValues);
 
// getting all the values
var getKeys = set1.keys();
 
// prints a SetIterator
// that contains {50, 30, 40, "Geeks", "GFG"}
console.log(getKeys);
  • Set.prototype.forEach() - 它为 Set 中的每个元素执行一次给定函数,按插入顺序。
    句法:
set1.forEach(callback[,thisargument]);

Parameter:
callback - It is a function which is to be executed for each element of the Set.
thisargument - Value to be used as this when executing the callback.

Returns: 
Undefined
  1. 回调函数提供了三个参数,如下所示:
    • 元素键
    • 元素值
    • 要遍历的Set 对象
  • Set.prototype[@@iterator]() - 它返回一个 Set 迭代器函数,默认为values()函数。
    句法:
set1[Symbol.iterator]();

Parameter:
No parameters

Returns: 
A Set iterator function and it is values() by default.

例子:

Javascript

// using Set.prototype[@@Iterator]()
var set1 = new Set(["sumit","sumit","amit","anish"]);
 
var getit = set1[Symbol.iterator]();
 
// Printing the values in the
// iterator "getit"
 
// prints {value: "sumit", done: false}
console.log(getit.next());
 
// prints {value: "amit", done: false}
console.log(getit.next());
 
// prints {value: "anish", done: false}
console.log(getit.next());
 
// prints {value: undefined, done: true}
console.log(getit.next());

设置操作:

  • subSet() – 如果Set ASet B的子集,则返回 true。
    如果集合A的所有元素也存在于集合 B中,则称集合 A集合 B的子集。
    现在让我们实现和使用子集函数。
    例子:

Javascript

// check whether the set on which the
// method is invoked is the subset of
// otherset or not
Set.prototype.subSet = function(otherSet)
{
    // if size of this set is greater
    // than otherSet then it can'nt be
    //  a subset
    if(this.size > otherSet.size)
        return false;
    else
    {
        for(var elem of this)
        {
            // if any of the element of
            // this is not present in the
            // otherset then return false
            if(!otherSet.has(elem))
                return false;
        }
        return true;
    }
}
 
// using the subSet function
 
// Declaring different sets
var setA = new Set([10, 20, 30]);
var setB = new Set([50, 60, 10, 20, 30, 40]);
var setC = new Set([10, 30, 40, 50]);
 
// prints true
console.log(setA.subSet(setB));
 
// prints false
console.log(setA.subSet(setC));
 
// prints true
console.log(setC.subSet(setB));
  • union() - 它返回一个由集合 A集合 B的并集组成的集合
    如果一个 Set 包含Set A的所有元素以及Set B的所有元素,则称它是两个集合的并集,但它不包含重复的元素。
    例如:如果集合 A集合 B中都存在一个元素,那么集合 A 和 B 的并集将包含该元素的单个副本。
    让我们实现和使用联合函数
    例子:

Javascript

// Perform union operation between
// called set and otherSet
Set.prototype.union = function(otherSet)
{
    // creating new set to store union
    var unionSet = new Set();
 
    // iterate over the values and add
    // it to unionSet
    for (var elem of this)
    {
        unionSet.add(elem);
    }
 
    // iterate over the values and add it to
    // the unionSet
    for(var elem of otherSet)
        unionSet.add(elem);
 
    // return the values of unionSet
    return unionSet;
}
 
// using the union function
// Declaring values for set1 and set2
var set1 = new Set([10, 20, 30, 40, 50]);
var set2 = new Set([40, 50, 60, 70, 80]); 
 
// performing union operation
// and storing the resultant set in
// unionSet
var unionSet = set1.union(set2);
 
// prints [10, 20, 30, 40, 50, 60, 70, 80]
console.log(unionSet.values());
  • intersection() – 返回Set ASet B的交集。
    如果包含同时存在于Set ASet B中的元素,则称 A Set 是Set A 和 Set B 的交集。
    让我们实现和使用交集函数
    例子:

Javascript

// Performs intersection operation between
// called set and otherSet
Set.prototype.intersection = function(otherSet)
{
    // creating new set to store intersection
    var intersectionSet = new Set();
 
    // Iterate over the values
    for(var elem of otherSet)
    {
        // if the other set contains a
        // similar value as of value[i]
        // then add it to intersectionSet
        if(this.has(elem))
            intersectionSet.add(elem);
    }
 
// return values of intersectionSet
return intersectionSet;               
}
// using intersection function
// Declaring values for set1 and set2
var set1 = new Set([10, 20, 30, 40, 50]);
var set2 = new Set([40, 50, 60, 70, 80]); 
 
// performing union operation
// and storing the resultant set in
// intersectionset
var intersectionSet = set1.intersection(set2);
 
// prints {40, 50}
console.log(intersectionSet.values());
  • difference() - 它返回包含Set ASet B差异的 Set。
    如果一个 Set 包含元素 e 的集合,这些元素存在于Set A但不存在于Set B中,则称它是Set A 和 B的差异。
    让我们实现和使用差异函数
    例子:

Javascript

// Performs difference operation between
// called set and otherSet
Set.prototype.difference = function(otherSet)
{
    // creating new set to store difference
     var differenceSet = new Set();
 
    // iterate over the values
    for(var elem of this)
    {
        // if the value[i] is not present
        // in otherSet add to the differenceSet
        if(!otherSet.has(elem))
            differenceSet.add(elem);
    }
 
    // returns values of differenceSet
    return differenceSet;
}
 
// using difference function
// Declaring values for set1 and set2
var set1 = new Set([10, 20, 30, 40, 50]);
var set2 = new Set([40, 50, 60, 70, 80]); 
 
// performing union operation
// and storing the resultant set in
// intersectionset
var differenceSet = set1.difference(set2);
 
// prints {10, 20, 30}
console.log(differenceSet);

JavaScript 以网页开发而闻名,但它也用于各种非浏览器环境。您可以按照这个 JavaScript 教程和 JavaScript 示例从头开始学习 JavaScript。