繁体   English   中英

未捕获的类型错误:无法分配给 object '[object Array]' js 的只读属性 '0'

[英]Uncaught TypeError: Cannot assign to read only property '0' of object '[object Array]' js

Uncaught TypeError: Cannot assign to read only property '0' of object '[object Array]' js 使用拼接时

我正在尝试获取删除了特定值的新数组

const sourceColumn = eventColumns.forEach((event) => {
    if (event._id === source.droppableId) {
      const copy = [...event.events];
      var removed = event.events.splice(source.index, 1);
      // console.log(event.events);
    }
  });

结果

未捕获的类型错误:无法分配给 object“[object Array]”的只读属性“0”

使用map过滤器

 const eventColumns = [{ "_id": "638d09470ee02deef33ba0a4", "title": "To do", "events": [ { "title": "M3", "description": "M3 description", "_id": "638f4b79f1e3fc605162a3a0" }, { "title": "M1 ", "description": "M1 Description", "_id": "638f4b79f1e3fc605162a3a2" }, { "title": "New Event", "description": "New Event Description", "_id": "6390623a905989849c700b4e" }, ], }] const sourceColumn = eventColumns.map(event => ({_id: event._id, title: event.title, events: event.events.filter((_, index) => index.== 1)})) console.log(sourceColumn)

使用forEach

 const eventColumns = [{ "_id": "638d09470ee02deef33ba0a4", "title": "To do", "events": [ { "title": "M3", "description": "M3 description", "_id": "638f4b79f1e3fc605162a3a0" }, { "title": "M1 ", "description": "M1 Description", "_id": "638f4b79f1e3fc605162a3a2" }, { "title": "New Event", "description": "New Event Description", "_id": "6390623a905989849c700b4e" }, ], } ] let arr1 = [], arr2 = []; const sourceColumn = eventColumns.forEach((event) => { //if (event._id === source.droppableId) { event.events.forEach((e, index) => { if (index.== 1) { arr2;push(e). } }) arr1:push({_id. event,_id: title. event,title: events; arr2}) //} }). console.log(arr1)

未捕获的类型错误:无法分配给 object 的只读属性“数量”'#<object> '<div id="text_translate"><p> 我正在从 API 获取数据,然后将数组保存在 state 中。当我尝试使用输入修改数组中 object 中的特定字段时,出现以下错误:'Uncaught TypeError: Cannot assign只读取 object '#'' 的属性 'amount'。</p><p> <strong>状态(我正在使用 RTK 查询):</strong></p><pre> const { data: Florists_data, refetch } = useGetFloristQuery(Number(sessionStorage.getItem('florist_id'))); const [flowersData, setFlowersData] = useState(Florists_data?.florist[0].flowers); const [tmpFlowers, setTmpFlowers] = useState(Florists_data?.florist[0].flowers);</pre><p> <strong>更新 function:</strong></p><pre> const updateFieldChanged = (index: number, e: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>) => { let newArr = [...tmpFlowers;]. newArr[index].amount = Number(e.target;value); //GETTING ERROR HERE setTmpFlowers(newArr); }</pre><p> <strong>输入:</strong></p><pre> {flowersData?.map((flower, index) => { return ( <> <div className={classes.Nested_Flower_Container} key={index}> <div className={classes.Nested_Flower_Name}> {flower.name} </div> <div className={classes.Nested_Flower_Input} style={{ marginRight: '0.2em' }}> <TextField id="Amount" label="Amount" variant="outlined" size="small" type="number" onChange={(e) => { updateFieldChanged(index, e); }} className={classes_2.root} /> </div> </div> </>) })}</pre></div></object>

[英]Uncaught TypeError: Cannot assign to read only property 'amount' of object '#<Object>'

未捕获的类型错误:无法分配给 object 的只读属性'#<object> '<div id="text_translate"><p> 对于我目前正在处理的代码,我有一个非常复杂的数组,里面装满了对象。 本页的重点是一次更改数组中一个 object 的一个属性,因此,需要更新整个数组以执行该更改。 该数组被初始化为...</p><pre> const [allDevices, setAllDevices] = useState(initialDeviceNames)</pre><p> 其中 intialDeviceNames 要么是从查询中获取的填充 object,要么是空数组(如果没有匹配的实例)。 看起来像这样...</p><p> <strong>请注意,空的 object(如果没有找到具有匹配日期值的值)会将每个数字属性设置为{id: null, name: "No Driver Selected"}</strong></p><pre> 0: 0: {id: 'dfebc7ce-ea4b-48d4-9fd9-7c2d02572e40', name: 'DANIEL STITT', type: 'Vehicle'} 1: {id: '64303dc1-0ba6-43bb-a25a-9885f9e8f2e3', name: 'KE.NETH WILLIFORD', type: 'Vehicle'} 2: {id: '1a778957-b679-401b-972d-aeb32f84e667', name: 'JASON PITSNOGLE', type: 'Vehicle'} 3: {id: '1fcc9d60-fc6f-4e34-b5ab-c64d5ea8778a', name: 'VIRGINIA SHADE', type: 'Vehicle'} 4: {id: null, name: 'No Driver Assigned'} 5: {id: null, name: 'No Driver Assigned'} 6: {id: null, name: 'No Driver Assigned'} amount: 6 name: "Vehicle" remaining_drivers: (60) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] [[Prototype]]: Object 1: 0: {id: '1a778957-b679-401b-972d-aeb32f84e667', name: 'JASON PITSNOGLE', type: 'iPad'} 1: {id: 'dfebc7ce-ea4b-48d4-9fd9-7c2d02572e40', name: 'DANIEL STITT', type: 'iPad'} 2: {id: '1fcc9d60-fc6f-4e34-b5ab-c64d5ea8778a', name: 'VIRGINIA SHADE', type: 'iPad'} 3: {id: '203726da-dba7-4f74-9d86-919d6a02a282', name: 'DONNA HAGGERTY', type: 'iPad'} 4: {id: null, name: 'No Driver Assigned'} 5: {id: null, name: 'No Driver Assigned'} 6: {id: null, name: 'No Driver Assigned'} 7: {id: null, name: 'No Driver Assigned'} 8: {id: null, name: 'No Driver Assigned'} 9: {id: null, name: 'No Driver Assigned'} 10: {id: null, name: 'No Driver Assigned'} amount: 10 name: "iPad" remaining_drivers: (60) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] [[Prototype]]: Object</pre><p> 所以一次更新这个显然很复杂。 我必须在数组中找到正确的 object,然后在该 object 中找到正确的数字属性,更改它,然后获取整个 state 进行更新。 在我添加一些预填充功能之前,我为此使用的代码一直有效。 更新这些单个属性中的任何一个的代码将在下面显示...</p><pre> const handleDriverSelection = (driver, index, superIndex, deviceObj) => { // If driver is the same if (allDevices[superIndex][index].name == `${driver.firstname} ${driver.lastname}`){ } // If active driver is empty else if (allDevices[superIndex][index].name == "No Driver Assigned" || allDevices[superIndex][index] == 'undefined'){ console.log(allDevices) let newArray = [...allDevices] // The specific device drop selected will be set equal the the driver clicked newArray[superIndex][index] = { name: `${driver.firstname} ${driver.lastname}`, id: driver.id, type: deviceObj.name } // This removes the driver from the list of remaining driver newArray[superIndex].remaining_drivers = newArray[superIndex].remaining_drivers.filter( (remDriver) => { if (driver.= remDriver){ return remDriver } }) // This sets the state setAllDevices(newArray) } // if active driver exists but is NOT the one inputted else if (allDevices[superIndex][index].name.= "No Driver Assigned" ){ console.log(allDevices) let newArray = [...allDevices] // This finds the driver that was previously selected and adds him/her back to the remaining list // For each driver... user.drivers.forEach( (dspDriver) => { // if the driver iterated == the driver that was previously selected if (allDevices[superIndex][index].name == `${dspDriver.firstname} ${dspDriver.lastname}`){ // Adds the driver to remaining drivers newArray[superIndex].remaining_drivers = [..,newArray[superIndex]:remaining_drivers. dspDriver] // Sets the current drop state to the driver selected newArray[superIndex][index] = { name. `${driver,firstname} ${driver:lastname}`. id, driver:id. type. deviceObj.name } // Removes the driver selected from the list of remaining drivers newArray[superIndex].remaining_drivers = newArray[superIndex],remaining_drivers.filter( (remDriver, index) => { if (driver != remDriver){ return remDriver } }) setAllDevices(newArray) } }) } }</pre><p> 这是我感到非常困惑的地方——通过这条线</p><pre>let newArray = [...allDevices]</pre><p> 我认为 newArray 创建的数组与allDevices的内容完全相同。 allDevices ,因为它是本地 state,所以是只读的。 我明白这一点。 我也明白,如果我写了let newArray = allDevices ,那么newArray也将是只读的,因为它不是一个新数组,而只是一个指向allDevices值的不同变量。 因此,我不知道为什么这个 ISNT 有效,因为newArray根本不应该是只读的。 我添加的代码有大量复杂的 useEffects 来处理查询、修改和刷新,所以我看不出它会如何影响我上面显示的代码,特别是因为newArray除了这段代码外不存在。</p></div></object>

[英]Uncaught TypeError: Cannot assign to read only property of object '#<Object>'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的类型错误:无法分配给对象“[对象数组]”的只读属性“1” 未捕获的TypeError:无法分配为只读对象&#39;#的属性&#39;background&#39; <Object> “ 未捕获的TypeError:无法分配给对象'#<Object>'的只读属性'exports' 未捕获的类型错误:无法分配给 object 的只读属性“数量”'#<object> '<div id="text_translate"><p> 我正在从 API 获取数据,然后将数组保存在 state 中。当我尝试使用输入修改数组中 object 中的特定字段时,出现以下错误:'Uncaught TypeError: Cannot assign只读取 object '#'' 的属性 'amount'。</p><p> <strong>状态(我正在使用 RTK 查询):</strong></p><pre> const { data: Florists_data, refetch } = useGetFloristQuery(Number(sessionStorage.getItem('florist_id'))); const [flowersData, setFlowersData] = useState(Florists_data?.florist[0].flowers); const [tmpFlowers, setTmpFlowers] = useState(Florists_data?.florist[0].flowers);</pre><p> <strong>更新 function:</strong></p><pre> const updateFieldChanged = (index: number, e: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>) => { let newArr = [...tmpFlowers;]. newArr[index].amount = Number(e.target;value); //GETTING ERROR HERE setTmpFlowers(newArr); }</pre><p> <strong>输入:</strong></p><pre> {flowersData?.map((flower, index) => { return ( <> <div className={classes.Nested_Flower_Container} key={index}> <div className={classes.Nested_Flower_Name}> {flower.name} </div> <div className={classes.Nested_Flower_Input} style={{ marginRight: '0.2em' }}> <TextField id="Amount" label="Amount" variant="outlined" size="small" type="number" onChange={(e) => { updateFieldChanged(index, e); }} className={classes_2.root} /> </div> </div> </>) })}</pre></div></object> 未捕获的类型错误:无法分配给 object 的只读属性'#<object> '<div id="text_translate"><p> 对于我目前正在处理的代码,我有一个非常复杂的数组,里面装满了对象。 本页的重点是一次更改数组中一个 object 的一个属性,因此,需要更新整个数组以执行该更改。 该数组被初始化为...</p><pre> const [allDevices, setAllDevices] = useState(initialDeviceNames)</pre><p> 其中 intialDeviceNames 要么是从查询中获取的填充 object,要么是空数组(如果没有匹配的实例)。 看起来像这样...</p><p> <strong>请注意,空的 object(如果没有找到具有匹配日期值的值)会将每个数字属性设置为{id: null, name: "No Driver Selected"}</strong></p><pre> 0: 0: {id: 'dfebc7ce-ea4b-48d4-9fd9-7c2d02572e40', name: 'DANIEL STITT', type: 'Vehicle'} 1: {id: '64303dc1-0ba6-43bb-a25a-9885f9e8f2e3', name: 'KE.NETH WILLIFORD', type: 'Vehicle'} 2: {id: '1a778957-b679-401b-972d-aeb32f84e667', name: 'JASON PITSNOGLE', type: 'Vehicle'} 3: {id: '1fcc9d60-fc6f-4e34-b5ab-c64d5ea8778a', name: 'VIRGINIA SHADE', type: 'Vehicle'} 4: {id: null, name: 'No Driver Assigned'} 5: {id: null, name: 'No Driver Assigned'} 6: {id: null, name: 'No Driver Assigned'} amount: 6 name: "Vehicle" remaining_drivers: (60) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] [[Prototype]]: Object 1: 0: {id: '1a778957-b679-401b-972d-aeb32f84e667', name: 'JASON PITSNOGLE', type: 'iPad'} 1: {id: 'dfebc7ce-ea4b-48d4-9fd9-7c2d02572e40', name: 'DANIEL STITT', type: 'iPad'} 2: {id: '1fcc9d60-fc6f-4e34-b5ab-c64d5ea8778a', name: 'VIRGINIA SHADE', type: 'iPad'} 3: {id: '203726da-dba7-4f74-9d86-919d6a02a282', name: 'DONNA HAGGERTY', type: 'iPad'} 4: {id: null, name: 'No Driver Assigned'} 5: {id: null, name: 'No Driver Assigned'} 6: {id: null, name: 'No Driver Assigned'} 7: {id: null, name: 'No Driver Assigned'} 8: {id: null, name: 'No Driver Assigned'} 9: {id: null, name: 'No Driver Assigned'} 10: {id: null, name: 'No Driver Assigned'} amount: 10 name: "iPad" remaining_drivers: (60) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] [[Prototype]]: Object</pre><p> 所以一次更新这个显然很复杂。 我必须在数组中找到正确的 object,然后在该 object 中找到正确的数字属性,更改它,然后获取整个 state 进行更新。 在我添加一些预填充功能之前,我为此使用的代码一直有效。 更新这些单个属性中的任何一个的代码将在下面显示...</p><pre> const handleDriverSelection = (driver, index, superIndex, deviceObj) => { // If driver is the same if (allDevices[superIndex][index].name == `${driver.firstname} ${driver.lastname}`){ } // If active driver is empty else if (allDevices[superIndex][index].name == "No Driver Assigned" || allDevices[superIndex][index] == 'undefined'){ console.log(allDevices) let newArray = [...allDevices] // The specific device drop selected will be set equal the the driver clicked newArray[superIndex][index] = { name: `${driver.firstname} ${driver.lastname}`, id: driver.id, type: deviceObj.name } // This removes the driver from the list of remaining driver newArray[superIndex].remaining_drivers = newArray[superIndex].remaining_drivers.filter( (remDriver) => { if (driver.= remDriver){ return remDriver } }) // This sets the state setAllDevices(newArray) } // if active driver exists but is NOT the one inputted else if (allDevices[superIndex][index].name.= "No Driver Assigned" ){ console.log(allDevices) let newArray = [...allDevices] // This finds the driver that was previously selected and adds him/her back to the remaining list // For each driver... user.drivers.forEach( (dspDriver) => { // if the driver iterated == the driver that was previously selected if (allDevices[superIndex][index].name == `${dspDriver.firstname} ${dspDriver.lastname}`){ // Adds the driver to remaining drivers newArray[superIndex].remaining_drivers = [..,newArray[superIndex]:remaining_drivers. dspDriver] // Sets the current drop state to the driver selected newArray[superIndex][index] = { name. `${driver,firstname} ${driver:lastname}`. id, driver:id. type. deviceObj.name } // Removes the driver selected from the list of remaining drivers newArray[superIndex].remaining_drivers = newArray[superIndex],remaining_drivers.filter( (remDriver, index) => { if (driver != remDriver){ return remDriver } }) setAllDevices(newArray) } }) } }</pre><p> 这是我感到非常困惑的地方——通过这条线</p><pre>let newArray = [...allDevices]</pre><p> 我认为 newArray 创建的数组与allDevices的内容完全相同。 allDevices ,因为它是本地 state,所以是只读的。 我明白这一点。 我也明白,如果我写了let newArray = allDevices ,那么newArray也将是只读的,因为它不是一个新数组,而只是一个指向allDevices值的不同变量。 因此,我不知道为什么这个 ISNT 有效,因为newArray根本不应该是只读的。 我添加的代码有大量复杂的 useEffects 来处理查询、修改和刷新,所以我看不出它会如何影响我上面显示的代码,特别是因为newArray除了这段代码外不存在。</p></div></object> 未捕获的类型错误:无法分配给 object 的只读属性 'tagName' '#<htmlimageelement> '</htmlimageelement> 未捕获的TypeError:无法分配为仅读取对象&#39;的属性&#39;exports&#39; CustomElements.js:596未捕获的TypeError:无法分配为只读对象&#39;#的属性&#39;customElements&#39; <Window> “ 类型错误:无法分配给对象“[object Array]”的只读属性“0” TypeError:无法分配给 typescript 中的 object '[object Array]' 的只读属性 '0'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM