未加星标

ImmutableJS: Creating Sets

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

A Set is similar to a List at first glance, but has a number of different properties and functions. In this tutorial, we’ll look at how to create a Set, and why you’d use a Set over a List.

Set

Although a Set is similar to a List, its differences make it more suitable for a variety of different problems. Foremost of these is that, unlike a List, a Set is a collection of unique values, and so cannot contain duplicate values. If you attempt to add a duplicate value to an existing Set, it is simply ignored, and no errors are thrown.

Other differences include functions such as union , intersect and subtract , which provide powerful Set manipulation operations that cannot easily (or performantly) be achieved with a List.

Here’s how to use it.

Create an Empty Set // Create an empty Set
const emptySet = Immutable.Set();
// Output:
emptySet; Create a Set of data: Set() vs Set.of()

As with all Immutable objects, you can create a Set of data using either the Set() constructor, or the Set.of() method, depending on the type of data you’re using to create the Set. Creating a new Set works very similarly to creating a new List:

Set.of() use when creating a Set from non-iterable data (e.g. function arguments, a javascript object, or a string you want interpreted as a whole string); Set() use when creating a Set from iterable data (e.g. an array, or an Immutable Iterable object (List, Map, Set, etc.), or a string that you want interpreted as a series of characters). Important:a JavaScript string is an iterable object , so if you create a Set of strings using Set("string") , you’ll actually get a Set of characters ( ['s', 't', 'r', 'i', 'n', 'g'] ). To make Immutable interpret a string as a non-iterable value, use Set.of("string") instead. See examples below. Create a new Set from… …a JavaScript array // Create a new Set from an array
const avengersArray = ['ironMan', 'captainAmerica', 'blackWidow'];
const avengersSet = Immutable.Set(avengersArray);
// Output:
avengersSet; …a JavaScript object

An object is not iterable, so we need to use Set.of() :

// Create a new Set from an object
const avengers = {
ironMan: 'Tony Stark',
captainAmerica: 'Steve Rogers',
blackWidow: 'Natasha Romanov'
};
const avengersSet = Immutable.Set.of(avengers);
// Output:
avengersSet; …just the keys of a JavaScript object

Rather than encoding an entire object as a Set, you can extract just its keys using Set.fromkeys() :

// Create a new Set from an object's keys
const avengers = {
ironMan: 'Tony Stark',
captainAmerica: 'Steve Rogers',
blackWidow: 'Natasha Romanov'
};
const avengersSet = Immutable.Set.fromKeys(avengers);
// Output:
avengersSet; …a set of function arguments

To create an Immutable Set of values that are passed in as a list of function arguments, use Set.of() .

// Create a new Set of strings
const avengersSet = Immutable.Set.of('ironMan', 'captainAmerica', 'blackWidow');
// Output:
avengersSet; …a JavaScript iterator

Same as with List , any ES6 iterable object, either built-in or user-defined, can be used to create a new Immutable Map.

// New Set from existing JavaScript iterator (Array.map example)
// Note: an ES6 object is not an iterator, so we'll use an array instead
const avengersArray = ['ironMan' , 'captainAmerica'];
const avengersSet = Immutable.Set(avengersArray.map(
(item, index) => ([ 'heroName' + index, item ]))
);
// Output:
avengersSet; …an Immutable List // Create a new Set from a List
const avengersList = Immutable.List(['ironMan', 'captainAmerica', 'blackWidow']);
const avengersSet = Immutable.Set(avengersList);
// Output:
avengersSet; …an Immutable Map // Create a new Set from a Map
const avengersMap = Immutable.Map({
ironMan: 'Tony Stark',
captainAmerica: 'Steve Rogers',
blackWidow: 'Natasha Romanov'
});
const avengersSet = Immutable.Set(avengersMap);
// Output:
avengersSet; Set operations: Union, Intersect and Subtract

Set provides a range of methods not available to other Immutable objects that can be used to manipulate data in ways that a standard Immutable merge() cannot accomplish.

These include Union , Intersect and Subtract . These operations are covered in the next article.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: JavaScriptJava
分页:12
转载请注明
本文标题:ImmutableJS: Creating Sets
本站链接:http://www.codesec.net/view/483834.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(31)