如何让setIn在immutable.js中设置一个数字键而不是数组?
How to make setIn set a number key instead of array in immutable.js?
我有这个代码:
let toDayKey = toDayKey.toString(); // 1
const deptId = deptId.toString(); // 3
const jobId = jobId.toString(); // 87
//Assuming copied.toJS() is any value like "copied job"
const calendar_job_add = calendar_job_removed.setIn([toDayKey,deptId,jobId], copied_job.toJS());
calendar_job* 是一个不可变的映射,我试图为使用 jobId
添加一个值作为键,即 87
而不是获取
的对象
calendar_job_add = {"1": { "3": {"87": "the copied job" } } }
我得到这个:
calendar_job_add = {"1": { "3": {
0: undefined,
1: undefined,
2: undefined,
3: undefined
...
..
.
87: "copied job"
} } }
它似乎是将它变成一个 Array 对象而不是一个 Object 并插入未定义的值。给出了什么?
非常感谢任何建议。
我只是做了一个解决方法,因为我在 documentation/internet 中找不到可以让我做我想做的事情。
我只需要使用 .toJS()
将输出对象变成 javascript 对象,并使用 object[jobId] = "copied job"
符号或 Object.assign({},{[jobId]: "copied job" })
添加一个值并将它们重新组合在一起。
很难判断您的代码出了什么问题。当我在 Map
上调用 setIn
并将其传递给数字键时,它会一直向下创建嵌套映射,无论我是对这些键使用实际的 number
还是像 [=17= 这样的字符串], '2'
, '3'
, 等等
const before = Immutable.Map({})
console.log('before:', before);
const withStringKeys = before.setIn(['1', '2', '3'], 'a')
console.log('with string keys:', withStringKeys);
const withNumberKeys = before.setIn([1, 2, 3], 'b')
console.log('with number keys:', withStringKeys);
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.9/immutable.js"></script>
我发现 运行 解决您遇到的问题的唯一方法是尝试 setIn
已经是 List
的问题。例如:
const before = Immutable.fromJS({ 1: { 2: [] } });
console.log('before:', before);
const withStringKeys = before.setIn(['1', '2', '3'], 'a')
console.log('with string keys:', withStringKeys);
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.9/immutable.js"></script>
我有这个代码:
let toDayKey = toDayKey.toString(); // 1
const deptId = deptId.toString(); // 3
const jobId = jobId.toString(); // 87
//Assuming copied.toJS() is any value like "copied job"
const calendar_job_add = calendar_job_removed.setIn([toDayKey,deptId,jobId], copied_job.toJS());
calendar_job* 是一个不可变的映射,我试图为使用 jobId
添加一个值作为键,即 87
而不是获取
的对象calendar_job_add = {"1": { "3": {"87": "the copied job" } } }
我得到这个:
calendar_job_add = {"1": { "3": {
0: undefined,
1: undefined,
2: undefined,
3: undefined
...
..
.
87: "copied job"
} } }
它似乎是将它变成一个 Array 对象而不是一个 Object 并插入未定义的值。给出了什么?
非常感谢任何建议。
我只是做了一个解决方法,因为我在 documentation/internet 中找不到可以让我做我想做的事情。
我只需要使用 .toJS()
将输出对象变成 javascript 对象,并使用 object[jobId] = "copied job"
符号或 Object.assign({},{[jobId]: "copied job" })
添加一个值并将它们重新组合在一起。
很难判断您的代码出了什么问题。当我在 Map
上调用 setIn
并将其传递给数字键时,它会一直向下创建嵌套映射,无论我是对这些键使用实际的 number
还是像 [=17= 这样的字符串], '2'
, '3'
, 等等
const before = Immutable.Map({})
console.log('before:', before);
const withStringKeys = before.setIn(['1', '2', '3'], 'a')
console.log('with string keys:', withStringKeys);
const withNumberKeys = before.setIn([1, 2, 3], 'b')
console.log('with number keys:', withStringKeys);
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.9/immutable.js"></script>
我发现 运行 解决您遇到的问题的唯一方法是尝试 setIn
已经是 List
的问题。例如:
const before = Immutable.fromJS({ 1: { 2: [] } });
console.log('before:', before);
const withStringKeys = before.setIn(['1', '2', '3'], 'a')
console.log('with string keys:', withStringKeys);
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/4.0.0-rc.9/immutable.js"></script>