编辑JavaScript数组,然后.map()将它们映射到另一个数组



我希望能够从一个数组中提取字符串或数字,然后将它们缩写为前两个或三个字母/数字,然后.map((将它们缩写到另一个数组。

以下解决方案可以缩写数字和字符串。

const maxLength = 2;
const theArray = [12345, 7736251, "abcdef", "foo", "baaaaar"];
const theResult = theArray.map(entry => String(entry).slice(0, maxLength));

如果你想保留类型,我可以修改代码来说明它。

最新更新