Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将不可变数组从字符串转换为数字_Javascript_Immutable.js - Fatal编程技术网

Javascript 将不可变数组从字符串转换为数字

Javascript 将不可变数组从字符串转换为数字,javascript,immutable.js,Javascript,Immutable.js,我有一个Vanilla JS数组,其中的数字是字符串 const stringArray = ['1', '2', '3', '4']; 如果我想将数组中的所有数字转换为intger,我只需要这样做 const integerArray = stringArray.map(Number); 现在,我有了一个不可变的JS列表 const immutableListStrings = Immutable.List(stringArray); 使用 immutableListStrings.ma

我有一个
Vanilla JS
数组,其中的数字是
字符串

const stringArray = ['1', '2', '3', '4'];
如果我想将数组中的所有数字转换为
intger
,我只需要这样做

const integerArray = stringArray.map(Number);
现在,我有了一个不可变的JS列表

const immutableListStrings = Immutable.List(stringArray);
使用

immutableListStrings.map(Number);

不将
字符串
转换为
整数
。为什么会这样?

map
通常通过列表值进行映射,并允许您在将其放入新的列表之前对其进行转换

map
不会转换当前列表

您只需创建一个新变量(即:ImmutableNumber)

conststringarray=['1','2','3','4'];
常量ImmutableListString=Immutable.List(stringArray);
常量immutableNumbers=immutableListStrings.map(数字);
console.log(不可变编号)

我不了解immutable.js,但在vanilla js
数组中。prototype.map
不会更改数组。就像使用标准map一样,immutable的
.map
返回一个新的
列表
()。事实上,库的全部目的是创建/使用不可变的数据结构,因此操作通常会返回一个新值。