\u003cp\u003e你可以使用以下代码来实现这个函数:\u003c/p\u003e\n\n\u003ccode\u003e\nfunction sortArray(a: string[], b: string[]): string[] {\n return b.sort((x, y) \u003d\u003e {\n const indexX \u003d a.indexOf(x);\n const indexY \u003d a.indexOf(y);\n return indexX - indexY;\n });\n}\n\nconst a \u003d ['w', 't', 'x', 'e', 'h', 'c'];\nconst b \u003d ['x', 't', 'c'];\nconst sortedB \u003d sortArray(a, b);\nconsole.log(sortedB); // 输出 [ 't', 'x', 'c' ]\n\u003c/code\u003e\n\n\u003cp\u003e在这个函数中,我们使用了sort方法对数组b进行排序。sort方法接受一个比较函数作为参数,该函数定义了排序的规则。在这个比较函数中,我们通过indexOf方法获取元素在数组a中的索引,然后根据索引的大小进行比较。最后返回排序后的数组b。\u003c/p\u003e


原文地址: https://www.cveoy.top/t/topic/pKbD 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录