javascript - how to limit the characters of an array
问题描述
I have this 'lista' array and each object returns a different value. but I have to treat this value.
example: the lista[1]
returns: [466.1637615180899, 'A#']
. But I want that it returns just ['466.1638', 'A#']
, so I need to show 8 string characters no numbers.
how can I do that?
const D = [440 * d, 'D']
const DS = [440 * ds, 'D#']
const E = [440 * e, 'E']
const ES = [440 * es, 'E#']
const F = [440 * f, 'F']
const FS = [440 * fs, 'F#']
const lista = [A, AS, B, BS, C, CS, D, DS, E, ES, F, FS]
if(semitons >= 0 && semitons < 13){
return lista[semitons]
}
解决方案
lista.map(val => ([val[0].toFixed(4), val[1]]))
如上所述,但我会使用 toFixed 方法,因为它更短更直接。
toFixed() 方法将数字转换为字符串,四舍五入到指定的小数位数。
推荐阅读
- angular - 使用 tick 和 fakeAsync 对 Observable 进行单元测试角度组件
- javascript - CKEditor5,如何测试简单插件的概念证明?
- intellij-idea - IntelliJ - 语言级别 5 不支持菱形类型和 Lambda 表达式
- javascript - 如何使用 jquery 访问 $(this) 关键字下的子元素
- javascript - Webpack 热重载非常慢
- asp.net-mvc - 剑道 UI 多重网格
- javascript - Angular Firestore 根据返回的布尔值显示一个按钮,用于检查 Firestore 文档是否存在
- sql - 如何创建连接两个表的视图以显示一个表中的哪些实体未连接到另一个表中的实体?
- symfony - 如何在 Symfony3.4 中通过用户名使用 Lock 组件
- qt - 如何测量 QML 文件中的导入时间?