Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编辑JavaScript数组,然后将它们映射到另一个数组_Javascript_Arrays - Fatal编程技术网

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

编辑JavaScript数组,然后将它们映射到另一个数组,javascript,arrays,Javascript,Arrays,我希望能够从一个数组中提取字符串或数字,然后将它们缩写为前两个或三个字母/数字,然后将它们.map()缩写为另一个数组。以下解决方案可以同时缩写数字和字符串 const maxLength = 2; const theArray = [12345, 7736251, "abcdef", "foo", "baaaaar"]; const theResult = theArray.map(entry => String(entry).slice(0, maxLength)); 如果您想保留该

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

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

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

如果您想保留该类型,我可以修改代码来解释它。

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

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

如果您想保留该类型,我可以修改代码以说明原因。

谢谢您,这很有效。谢谢您,这很有效。请提供您已经尝试过的内容的详细信息,否则as请求代码被视为离题。请提供您已经尝试过的内容的详细信息,否则,as请求代码被认为是离题的。