Home > Web Front-end > JS Tutorial > body text

Introduction to the use of js data structure type extension immutable-js (code example)

不言
Release: 2018-09-17 14:43:28
Original
2023 people have browsed it

This article brings you an introduction to the use of js data structure type extension immutable-js (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

Compared with strongly typed languages ​​such as java and .net, one big difference between js and js is that the data structures only have array and object, and they are all dynamically variable, while java has List, Set, Map and other data structures. Therefore, in order to use these data structures in js, immutable-js came into being.

1. immutable-js

immutable-js extends immutable collections in JavaScript, that is, data types that cannot be changed once created. This simplifies application development, defenseless replication, enables more advanced memory schemes, and uses simpler logic to check for updates. Persistent data provides modifiable APIs that do not update data in place, but generate new updated data.

2. Provided data types

1. List: ordered index collection, similar to Array in JavaScript

2. Map: unordered key-value pair ((key , value) pairs) set

3. OrderedMap: ordered Map

4. Set: set without duplicate values

5. OrderedSet: ordered Set

6. Stack: An index collection that supports element addition and removal.

7. Range(): Returns a Seq.Indexed collection filled with step size from start to end, start default value is 0, the default value of step is 1, and the default value of end is infinity. If start = end, an empty collection is returned.

8. Repeat(): Returns a Seq.Indexed collection that repeats value times times. If times is undefined, returns a Seq collection of infinite value values.

9. Record: Similar to JavaScript's Object, but only receives specific strings as keys and has default values.

10. Seq: Allows high-order collection functions to be used without intermediate collections ( A collection of lazy operations for efficient chain calls, such as map, filter)

11. Collection: the base class of all data structures

3. API provided

3.1 fromJS: Convert a js data to immutable-js type data

const { fromJS, isKeyed } = require('immutable@4.0.0-rc.9');
fromJS({ a: {b: [10, 20, 30]}, c: 40}, function (key, value, path) {
  console.log(key, value, path)
  return isKeyed(value) ? value.toOrderedMap() : value.toList()
})

> "b", [ 10, 20, 30 ], [ "a", "b" ]
> "a", {b: [10, 20, 30]}, [ "a" ]
> "", {a: {b: [10, 20, 30]}, c: 40}, []
Copy after login

3.2 is: Compare two objects

const { Map, is } = require('immutable@4.0.0-rc.9')
const map1 = Map({ a: 1, b: 1, c: 1 })
const map2 = Map({ a: 1, b: 1, c: 1 })
assert.equal(map1 !== map2, true)
assert.equal(Object.is(map1, map2), false)
assert.equal(is(map1, map2), true)
Copy after login

The above is the detailed content of Introduction to the use of js data structure type extension immutable-js (code example). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template