简体   繁体   English

数组中的散布操作出错。 TS1005:“,”应为。 打字稿

[英]Error with spread operation in array. TS1005: ',' expected. TypeScript

I cannot figure out what I missed on line row.sections[SECTION_ID . 我无法弄清在row.sections[SECTION_ID上错过了什么。 It always show me a typo error ','... 它总是向我显示错字错误','...

FAQ: sections - is an array with objects inside. 常见问题解答: sections是一个内部包含objects的数组。 In this case I'm trying modify the specific object of the sections founded by custom flag SECTION_ID. 在这种情况下,我尝试修改由自定义标记SECTION_ID创建的部分的特定对象。

PS PS

I also tried to put row.sections[SECTION_ID] inside an extra brackets [] , but unfortunately it does not help... Any solutions? 我也尝试将row.sections[SECTION_ID]放在一个额外的方括号[] ,但不幸的是,这无济于事。

  rows: state.rows.map(
    row =>
      row.ID === action.rowID
        ? {
            ...row,
            sections: [
              ...row.sections,
              row.sections[SECTION_ID]: { // error is here
                ...row.sections[SECTION_ID],
                data: {
                  ...// some data
                }
              }
            ]
          }
        : row
  )

You cannot mutate some element inside array by spread operation in such way. 您无法通过spread操作以这种方式对array内的某些element突变。 Using this approach you'll jus add a new, mutated element to the same array each time. 使用这种方法,您将每次都将一个新的,变异的element添加到同一array So, it you want to make it right, you need to use the map iterator instead: 因此,如果您想使其正确,则需要使用map迭代器:

rows: state.mymaps.rows.map(
    row =>
      row.ID === action.rowID
        ? {
            ...row,
            sections: row.sections.map(
              (section, index) =>
                index === JOIN_SECTION_ID
                  ? {
                      ...section,
                      data: {
                        ...section.data
                      }
                   } : section
             )
          } : row
)

If you're trying to replace an element at a certain index of the array without mutating the array, you can make a shallow copy of the array, and then set the value at that index. 如果您试图替换数组某个索引处的元素而不更改数组,则可以对数组进行浅表复制,然后在该索引处设置值。 For example: 例如:

state.rows.map((row) => {
  if (rowID !== action.rowID) {
    return row;
  }
  const sectionCopy = [...row.sections];
  sectionCopy[SECTION_ID] = {
    ...row.sections[SECTION_ID],
    data: {
      // some data
    },
  };
  return {
    ...row,
    sections: sectionCopy,
  };
});

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

相关问题 打字稿错误TS1005:':'预期。使用Object.assign() - Typescript error TS1005: ':' expected. with Object.assign() 打字稿错误TS1005:';' 预期。 TS v1.8.10 - Typescript error TS1005: ';' expected. TS v1.8.10 打字稿错误 TS1005: ';' 预期 - Typescript error TS1005: ';' expected 将Angular2-rc1升级到Angular2-rc2,面向TypeScript','。 (TS1005) - Upgrade Angular2-rc1 to Angular2-rc2, facing TypeScript ',' expected. (TS1005) 错误 TS1005: ',' expected, failing to compile angular 6 project - error TS1005: ',' expected, failing to compile angular 6 project TS1005 错误,“,”预期为 simple.forEach 循环 - TS1005 error, ','expected with simple .forEach loop 错误:src/app/components/button/button.component.ts:11:27 - 错误 TS1005: '(' 预期 - Error: src/app/components/button/button.component.ts:11:27 - error TS1005: '(' expected 为什么我得到 ':' expected.ts(1005) 作为 Typescript 错误? - Why do I get ':' expected.ts(1005) as a Typescript error? 当我在 discord 中创建机器人时,为什么我在 Visual Studio Code 中得到“','预期的 ts1005” - Why i get “ ',' expected ts1005” in Visual Studio Code when i am creating a bot in discord 获取:错误 TS1128:预期的声明或语句。 在 ReactJS 和 TypeScript 应用程序中 - Getting: error TS1128: Declaration or statement expected. In ReactJS & TypeScript app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM