首页 > 解决方案 > 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]
      }

标签: javascriptarraysreactjstypescriptmath

解决方案


lista.map(val => ([val[0].toFixed(4), val[1]]))

如上所述,但我会使用 toFixed 方法,因为它更短更直接。

toFixed() 方法将数字转换为字符串,四舍五入到指定的小数位数。


推荐阅读