簡體   English   中英

TypeError:無法分配給 object '[object Array]' 的只讀屬性 'x'

[英]TypeError: Cannot assign to read only property 'x' of object '[object Array]'

我嘗試了很多方法來改變它,不管它是如何在其他 Stack Overflow 帖子上完成/解釋的。 我有一個對象數組,我正在嘗試通過數組映射/循環將其中一個對象移動到數組的前面。

這是我得到的:

const [users, setUsers] = useState<User[]>([]);
const [primaryUser, setPrimaryUser] = useState<User>({
  id: 0;
  first_name: "",
  last_name: ""
})

useEffect(() => {
  users.map((user, i) => {
    if(user.id === 12345) {
      users.splice(i, 1);
      users.unshift(user)

      setPrimaryUser(user);
    }
  });

  setUsers(users);
}, [])

無論我是如上所示的 map 還是使用 for 循環,我總是會收到以下錯誤: TypeError: Cannot assign to read only property 'x' of object '[object Array]'

非常感謝任何建議/幫助。 提前致謝!

我看到代碼在 React 中使用,因此最好避免就地修改users數組(例如使用.unshift().splice() ),因為 React 不會看到差異並且不會重新渲染。

在 React 中,為之前的值創造新的價值是很常見的做法。

我認為您可以通過兩次過濾users數組來實現目標(過濾方法創建一個副本而不影響原始數組):

  • ID 為 12345 的用戶 - 只需要一個並將其存儲在primaryUser變量中
  • ID 不是 12345 的用戶

並結合這些 arrays:

useEffect(() => {
  const primaryUserID = 12345;
  const [ primaryUser ] = users.filter(user => user.id === primaryUserID)
  setPrimaryUser(primaryUser);

  // Construct array from 'primaryUser' and the rest of users
  const newUsers = [
     primaryUser,
     ...users.filter(user => user.id !== primaryUserID),
  ];
  setUsers(newUsers);
}, [])

多虧了 VLAZ 和 Vitalii(很棒的文章),我才能夠讓它工作。 這是我的解決方案:

useEffect(() => {
  let tempUsers = [...users];

  tempUsers.map((user, i) => {
    if(user.id === 12345) {
      tempUsers.splice(i, 1);
      tempUsers.unshift(user)

      setPrimaryUser(user);
    }
  });

  setUsers(tempUsers);
}, [])

謝謝你!

遍歷執行 switch 語句的數組返回 TypeError: Cannot assign to read only property 'location' of object '#<object> '<div id="text_translate"><p> 我有一個只讀數組,我復制它成為一個可變數組let mutableForecast = [...forecast]我正在使用這個新數組並用forEach迭代它,這樣我就可以改變數組。 我試圖使用帶有switch語句的一些流控制,但我收到TypeError: Cannot assign to read only property 'location' of object '#&lt;Object&gt;'</p><pre> let mutableForecast = [...forecast] mutableForecast.forEach((obj, i) =&gt; { switch (obj.location) { case obj.location === "BRITISH_COLUMBIA": obj.location = "BC" break; default: obj.location = "oother" } })</pre><p> 這里有什么問題? 我看過<a href="https://stackoverflow.com/questions/4689856/how-to-change-value-of-object-which-is-inside-an-array-using-javascript-or-jquer" rel="nofollow noreferrer">這個</a>、 <a href="https://stackoverflow.com/questions/57812091/cannot-assign-to-read-only-property" rel="nofollow noreferrer">這個</a>、 <a href="https://stackoverflow.com/questions/44288164/cannot-assign-to-read-only-property-name-of-object-object-object" rel="nofollow noreferrer">這個</a>和其他一些,但找不到答案。</p><p> 這是我復制之前預測數組的樣子<a href="https://i.stack.imgur.com/AnUQx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AnUQx.png" alt="在此處輸入圖像描述"></a></p></div></object>

[英]Iterating through a an array executing a switch statement returns TypeError: Cannot assign to read only property 'location' of object '#<Object>'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 未捕獲的類型錯誤:無法分配給對象“[對象數組]”的只讀屬性“1” TypeError: Cannot assign to read only property '0' of object '[object Array]' 如何解決? 遍歷執行 switch 語句的數組返回 TypeError: Cannot assign to read only property 'location' of object '#<object> '<div id="text_translate"><p> 我有一個只讀數組,我復制它成為一個可變數組let mutableForecast = [...forecast]我正在使用這個新數組並用forEach迭代它,這樣我就可以改變數組。 我試圖使用帶有switch語句的一些流控制,但我收到TypeError: Cannot assign to read only property 'location' of object '#&lt;Object&gt;'</p><pre> let mutableForecast = [...forecast] mutableForecast.forEach((obj, i) =&gt; { switch (obj.location) { case obj.location === "BRITISH_COLUMBIA": obj.location = "BC" break; default: obj.location = "oother" } })</pre><p> 這里有什么問題? 我看過<a href="https://stackoverflow.com/questions/4689856/how-to-change-value-of-object-which-is-inside-an-array-using-javascript-or-jquer" rel="nofollow noreferrer">這個</a>、 <a href="https://stackoverflow.com/questions/57812091/cannot-assign-to-read-only-property" rel="nofollow noreferrer">這個</a>、 <a href="https://stackoverflow.com/questions/44288164/cannot-assign-to-read-only-property-name-of-object-object-object" rel="nofollow noreferrer">這個</a>和其他一些,但找不到答案。</p><p> 這是我復制之前預測數組的樣子<a href="https://i.stack.imgur.com/AnUQx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AnUQx.png" alt="在此處輸入圖像描述"></a></p></div></object> TypeError:無法分配給字符串的只讀屬性“0” 類型錯誤:無法讀取數組內未定義狀態對象的屬性“map”? Jest - TypeError:無法讀取未定義的屬性“[object Array]” TypeError:無法分配給 React JS 中的只讀屬性 Javascript中的數組錯誤:未捕獲的TypeError:無法讀取未定義的屬性&#39;x&#39; 如何在React中的Array中修復TypeError“無法讀取null的屬性&#39;x&#39;” 未捕獲的TypeError:無法讀取未定義的屬性“ X”(數組)
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM