Reactjs 将千个分隔符添加到chartist.js

Reactjs 将千个分隔符添加到chartist.js,reactjs,chartist.js,Reactjs,Chartist.js,所以我尝试在图表数字中添加1000个分隔符,使用npm软件包ChartList制作。我试图实现的方式是: const data = { // A labels array that can contain any sort of values labels: this.props.labels.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "."), // Our

所以我尝试在图表数字中添加1000个分隔符,使用npm软件包ChartList制作。我试图实现的方式是:


    const data = {
          // A labels array that can contain any sort of values
          labels: this.props.labels.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "."),
          // Our series array that contains series objects or in this case series data arrays
          series: [this.props.data]
        };

我试图更改包本身,但每次出现下一个错误时:


    Uncaught TypeError: Cannot assign to read only property 'length' of object '[object String]'
        at String.push (<anonymous>)
        at Object.Chartist.normalizeData (chartist.js:400)
        at constr.createChart (chartist.js:3387)
        at constr.initialize (chartist.js:1940)

但是它表明data.labels(来自npm包)不是一个函数

编辑1: 如果我使用console.log(this.props.labels),那么
我粘贴在那里的图形编号带有一个包名ChartList插件工具提示,因此可能我必须在那里更改一些东西,我不知道。

它正在尝试运行
。按字符串而不是数组。我假设标签应该是一个数组,但是
.replace
将返回一个字符串

也许您打算使用将标签转换回数组。 从toString()可以得到一个字符串,该字符串是数组值的逗号分隔列表。因此,您必须使用逗号再次拆分它。比如说

labels: this.props.labels.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ".").split(',')
编辑

如果数组值有逗号,则需要使用另一个分隔符
.join
,而不是
.toString

    this.props.labels.join('$').replace(/\B(?=(\d{3})+(?!\d))/g, ".").split('$')
最终编辑

如果您不是在向数组中实际添加值,那么通过元素进行映射会更干净!当我再次阅读你的问题时,我意识到这有点晚了

this.props.labels.map(label => label.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "."))

它没有显示错误,但数字没有逗号。也许我没有把它放在应该放的地方?如果数组中的数字有字符“,”in,那么split(“,”)将为每个逗号创建额外的不需要的元素。因此,2,5将被拆分为数组中的两个元素。尝试使用.join而不是toString来控制分隔字符。(请参阅编辑)如果我尝试最后一次编辑,它会显示错误:“label.replace不是函数”@AlexandraTiganciuc Oh,因为每个元素都是一个数字而不是字符串。。。这个.props.labels.map(label=>label.toString().replace(/\B(?=(\d{3})+(?!\d))/g,“.”)我发现了一个问题:)我试图控制日志标签,因为它没有做任何事情,标签只是索引,如果我控制了console.log label.value,那么它就是未定义的
this.props.labels.map(label => label.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "."))