简体   繁体   English

我需要如何对数字名称 JS 进行排序。 而且我不能使用循环。 只有标准的数组方法

[英]How I need to Sorts digit names JS. And I cant use loops. Only standard Array methods

Examples:例子:

 *   [] => []
 *   [ 'nine','one' ]                 => [ 'one', 'nine' ]
 *   [ 'one','two','three' ]          => [ 'one','two', 'three' ]
 *   [ 'nine','eight','nine','eight'] => [ 'eight','eight','nine','nine']
 *   [ 'one','one','one','zero' ]     => [ 'zero','one','one','one' ]
 */

I suppose you don't know how to even start, that's why you didn't even try.我想你甚至不知道如何开始,这就是你甚至没有尝试的原因。

Anyway, here is one of approches, how it can be done:无论如何,这是一种方法,它是如何完成的:

 const input = ['nine','eight','nine','eight', 'one','two','three'] const digitsComparator = (a, b) => { const mapper = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] return mapper.indexOf(a) - mapper.indexOf(b) } const res = input.sort(digitsComparator) console.log(res)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要使用循环找出数组的每个字符串中有多少个元音字母。 看不出我做错了什么 - I need to find how many vowels are in each string of the array using loops. Can't see what I'm doing wrong 我对 JS 中的循环有疑问。 我是新手 - I have a query about Loops in JS. I am a newbie JS。 如何调用遍历数组的 function? - JS. How do I call a function that iterates through an array? 为什么我们不能在AngularJS中使用JS标准方法 - Why cant we use JS standard methods in AngularJS 异步 Function,JS 的问题。 我可以使用 Promise 吗? - Problem with Asynchronous Function, JS. Can I use Promises? 我对递归在 js 中的工作方式感到困惑。 下面的例子 - I got confused with how recursion works in js. Example below 反应JS。 如何在对象内部显示图像? - React JS. How can I display the image inside the object? 我需要在 JS 中的 url 旁边添加 HTML 的“选择/选项”的“值”。 你知道怎么做吗? - I need to add “value” of the “select / option” of HTML next to the url that is in the JS. Do you have an idea how to do it? JS。 如何通过与变量连接来创建正则表达式模式 - JS. How do I create a regex pattern by concatenation with variables 如何在反应 js 中获得多个 select 选项? - How can i get multiple select options in react js.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM