简体   繁体   中英

Removing and adding quotes from each string in an array - Javascript

I want to remove and add quotes in array string.

I am trying this .

dataPoint[shardIndex] = '{y:'+actualSize+', '+'label:'+shardName+'}';

var datapoint returns following result .

Array(3) [ 
     "{
          y:33.33282469150909, 
          label:shard1
      }", 
     "{
          y:33.33282469150909, 
          label:shard2
      }", 
     "{
          y:33.334350570415694, 
          label:shard3
      }" 
 ]

var datapoint returns above but I want the following result .

[
  {
       y:33.33282469150909, 
       label:"shard1"
  }, 
  {
       y:33.33282469150909, 
       label:"shard2"
  }, 
  {
       y:33.334350570415694, 
       label:"shard3"
  }
]

The problem here is that you are adding strings in your array. Instead, you should add objects.

The correct syntax for this should be.

dataPoint[shardIndex] = {y: actualSize, label: shardName};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM