将数字数组转换为书面形式-例如0->“零”

时间:2018-10-17 18:10:55

标签: javascript angular

如何接受数字数组并以书面形式返回数字数组,例如

[0,0,6,2,7]→[“零”,“零”,“六个”,“两个”,“七个”]

我想输入和返回值数组而不是一个值吗?

<div>
    <input type="text" [(ngModel)]="number" placeholder="Input Number"/>
    <div id="word">{{words[number]}}</div>
</div>


{{numbers[number]}}





<input type="text" [(ngModel)]="stringOfNumbers" placeholder="Input Number"/>
{{stringOfNumbers}}
<br>
{{arrayOfNumbers}}




words= ['zero','One','Two','Three','Four','Five','Six','seven','eight'];

stringOfNumbers = "1,2,3,4";
arrayOfNumbers = this.stringOfNumbers .split(',');

2 个答案:

答案 0 :(得分:1)

我建议您为此使用中间件(请查看以下答案:Convert digits into words with JavaScript,这非常硬。每次输入值更改并遍历数组时,都添加一个事件侦听器。

document.getElementsByTagName("input")[0].addEventListener('change', function(){
    alert("Input changed!");
    // Code goes here
});

答案 1 :(得分:1)

改善我的评论:({@ {3}}突如其来)

//The .html
<input [ngModel]="numbers" (ngModelChange)="calculeNumber($event)">
{{result|json}}

//The .ts
export class AppComponent  {
  numbers:string;
  result:string[]=[];
  words= ['zero','One','Two','Three','Four','Five','Six','seven','eight'];
  calculeNumber(  numbers:any){
    this.result=numbers.split(',').map(x=>
    {

      return this.words[+x]
    });

  }
}