由不同物件組成的原始陣列(form[id].values.sections)
(3) [{…}, {…}, {…}]
1. 0:
1. attachments: Array(0)
1. length: 0
2. [[Prototype]]: Array(0)
2. body: "" // How do i update the values here without the original array being converted into an object
3. [[Prototype]]: Object
2. 1: {body: '', attachments: Array(0)}
3. 2: {body: '', attachments: Array(0)}
4. length: 3
5. [[Prototype]]: Array(0)
如果我使用以下方法更新body,它會變成一個物件,如下所示
updatedSections = {
...form[id].values.sections,
0: {
body: contentBody, attachments: form[id].values.sections[0].attachments,
},
};
updatedSection 成為一個物件。
{0: {…}, 1: {…}, 2: {…}}
1. 0:
1. attachments: Array(0)
1. length: 0
2. [[Prototype]]: Array(0)
2. body: "<p>Content update here</p>"
3. [[Prototype]]: Object
2. 1: {body: '', attachments: Array(0)}
3. 2: {body: '', attachments: Array(0)}
[[Prototype]]: Object
uj5u.com熱心網友回復:
您已通過使用將陣列轉換為物件
updatedSections = { ... }
如果您希望它保持為陣列,則需要使用陣串列示法,例如
updatedSections = [
{
body: contentBody,
attachments: form[id].values.sections[0].attachments,
},
...form[id].values.sections.slice(1)
]
uj5u.com熱心網友回復:
是的,它變成了一個物件。因為 updatedSections 是一個物件。(您已使用 {} 對其進行初始化)。
updatedSections = { // <-- here
...form[id].values.sections,
0: {
body: contentBody, attachments: form[id].values.sections[0].attachments,
},
}; // <-- here
下面是訪問陣列中 body 屬性的正確方法。
form[id].values.sections[0].body = <your_new_content>;
如果您不想直接更新原始陣列,請復制該sections陣列并更新其中所需的屬性。希望這是有道理的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/515655.html
標籤:javascript反应
