[英]Immutable JS: Set property in object within array within object
假設我有一個這樣的對象:
const document = {
items: [
{ notes: ['a', 'b'] },
{ notes: ['y', 'z'] },
]
}
我想使用 Immutable JS 在一個項目上設置一個 notes 數組。 基本上做一個unshift,但不可變的。
這是我正在嘗試的:
const immutableDocument = Immutable.fromJs(document);
immutableDocument.setIn(['items[index]', 'notes'], [newNote, ...oldNotes]);
它不起作用,我在文檔中沒有看到任何關於通過 Immutable 對象中的某個索引(或謂詞)訪問數組的內容。
我將如何實現這一點?
更新:基於對這個問題的回答,我還嘗試了immutableDocument.setIn(['items', index, 'notes'], [newNote, ...oldNotes])
,這似乎也不起作用。
這有效:
const newDocument = Immutable.setIn(immutableDocument, ['items', index, 'notes'], [newNote, ...oldNotes]);
不完全確定為什么另一個根據文檔不起作用,但是嘿,我繼續前進。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.