Javascript data structure collection to array, array to collection and other operations

Array and collection conversion

Array to collection

let arr = ["A", "B", "C", "A", "C", "C", "D"];

let set = new Set(arr);

console.log(set);

//Set to array

arr = [...set];

console.log(arr);

 

//The attribute collection name of the collection. size is the length of the collection

console.log(set.size);

ES6 collection

Multiple data can be stored without duplicate data (congruent matching)

Declare collection (create collection)

let set = new Set();

Collection properties and methods

add add data

Note: add will return the new collection after adding (chain call)

set.add(2).add(3).add(4).add(true);

console.log(set);

 

Empty collection

set.clear();

console.log(set);//{}

 

delete

Writing format: collection name.delete (deleted content)

set.delete(2);

console.log(set); //{3,4,true}

 

Find

Writing format: collection name.has (search content) return Boolean

console.log(set.has(2)); //false

console.log(set.has(3)); //true

console.log(set);

 

for (let items of set.keys()) {

console.log(`keys:${items}`);

}

 

for (let items of set.values()) {

console.log(`values:${items}`);

}

 

Guess you like

Origin blog.csdn.net/qq_46462137/article/details/111499464