Files
chat/client/shared/utils/array-helper.ts
2026-04-25 16:36:34 +08:00

22 lines
472 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import _flatten from 'lodash/flatten';
/**
* 类似于join但是返回一个数组
* join会将元素强制转化为字符串
*
* 改函数可以用于join ReactNode
*
* @example joinArray([1, 2, 3], '5') => [1, '5', 2, '5', 3]
*/
export function joinArray<T, K>(arr: T[], separator: K): (T | K)[] {
return _flatten(
arr.map((item, i) => {
if (i === 0) {
return [item];
} else {
return [separator, item];
}
})
);
}