Repository: element-plus/element-plus
File: packages/components/table/src/store/expand.ts (TypeScript)

Code before:
    const data = watcherData.data.value || []
    const rowKey = watcherData.rowKey.value
    if (defaultExpandAll.value) {
      expandRows.value = instance.store.states.rowExpandable.value
        ? data.filter(canRowExpand)
        : data.slice()
    } else if (rowKey) {
      // TODO：这里的代码可以优化
      const expandRowsMap = getKeysMap(expandRows.value, rowKey)
      expandRows.value = data.filter((row, index) => {
        const rowId = getRowIdentity(row, rowKey)
        return !!expandRowsMap[rowId] && canRowExpand(row, index)
      })
    } else {
      expandRows.value = []
    }
  }

  const toggleRowExpansion = (row: T, expanded?: boolean) => {
    const dataArr = watcherData.data.value || []
    const rowKey = watcherData.rowKey.value
    const rowIndex = rowKey
      ? dataArr.findIndex(
          (r) => getRowIdentity(r, rowKey) === getRowIdentity(row, rowKey)
        )
      : dataArr.indexOf(row)
    if (!canRowExpand(row, rowIndex)) return

    const changed = toggleRowStatus(
      expandRows.value,
      row,
      expanded,
      undefined,
      undefined,
      undefined,
      watcherData.rowKey.value
    )
    if (changed) {
      instance.emit('expand-change', row, expandRows.value.slice())
    }
  }

  const setExpandRowKeys = (rowKeys: (string | number)[]) => {
    instance.store.assertRowKey()
    // TODO：这里的代码可以优化
    const data = watcherData.data.value || []
    const rowKey = watcherData.rowKey.value
    const keysMap = getKeysMap(data, rowKey)
    expandRows.value = rowKeys.reduce((prev: T[], cur) => {
      const info = keysMap[cur]
      if (info && canRowExpand(info.row, info.index)) {

Review comment (suggestion):
It seems that we don't need to distinguish between two cases here; we should be able to just use `indexOf`. 🤔 

Code after:
    const data = watcherData.data.value || []
    const rowKey = watcherData.rowKey.value
    if (defaultExpandAll.value) {
      expandRows.value = instance.store.states.rowExpandable.value
        ? data.filter(canRowExpand)
        : data.slice()
    } else if (rowKey) {
      // TODO：这里的代码可以优化
      const expandRowsMap = getKeysMap(expandRows.value, rowKey)
      expandRows.value = data.filter((row, index) => {
        const rowId = getRowIdentity(row, rowKey)
        return !!expandRowsMap[rowId] && canRowExpand(row, index)
      })
    } else {
      expandRows.value = []
    }
  }

  const toggleRowExpansion = (row: T, expanded?: boolean) => {
    const dataArr = watcherData.data.value || []
    const rowIndex = dataArr.indexOf(row)
    if (rowIndex > -1 && !canRowExpand(row, rowIndex)) return

    const changed = toggleRowStatus(
      expandRows.value,
      row,
      expanded,
      undefined,
      undefined,
      undefined,
      watcherData.rowKey.value
    )
    if (changed) {
      instance.emit('expand-change', row, expandRows.value.slice())
    }
  }

  const setExpandRowKeys = (rowKeys: (string | number)[]) => {
    instance.store.assertRowKey()
    // TODO：这里的代码可以优化
    const data = watcherData.data.value || []
    const rowKey = watcherData.rowKey.value
    const keysMap = getKeysMap(data, rowKey)
    expandRows.value = rowKeys.reduce((prev: T[], cur) => {
      const info = keysMap[cur]
      if (info && canRowExpand(info.row, info.index)) {
        prev.push(info.row)
      }
      return prev
    }, [])
  }