-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* 返回对象数组中指定的一列。 | ||
* | ||
* @param arr 要操作的对象数组 | ||
* @param columnKey 列的键值 | ||
* @param [indexKey] 作为返回对象的键的列,若不设置,则返回数组 | ||
* @returns 列对象或数组 | ||
*/ | ||
export default function column< | ||
O extends Record<string, any>, | ||
CK extends keyof O, | ||
IK extends keyof O, | ||
>( | ||
arr: O[], | ||
columnKey: CK, | ||
indexKey?: IK | ||
): IK extends undefined ? Array<O[CK]> : Record<O[IK], O[CK]> { | ||
return arr.reduce<any>( | ||
(res, item, index) => { | ||
res[indexKey ? item[indexKey] : index] = item[columnKey] | ||
return res | ||
}, | ||
indexKey ? {} : [] | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters