简体   繁体   English

Javascript D3对象字符串转换为数字

[英]Javascript d3 object string to number

I have a csv and i am trying to convert certain data to integers when i parse the object it is still showing up in the console as a string. 我有一个csv,当我解析对象仍以字符串形式显示在控制台中时,我正在尝试将某些数据转换为整数。

d3.csv('somecsv.csv' function(response){
  var data = response.map(function(item){
    var newItem{};
    newItem[parseInt("Column with spaces 1")] = item["Column with spaces 1"];
// This gives a Nan error
// even when i do 
//   newItem["Column with spaces 1"] = item["Column with spaces 1"];
//   parseInt(newItem["Column with spaces 1"])
   console.log(newItem)
//  This displays a string. It never converted to an Int.

When i console.log the data is stored in an object that looks like this 当我console.log数据存储在一个看起来像这样的对象

Object { "Column with spaces 1": "99", "Column with spaces 2": "37"  }

I want to convert it so it the value is a number not string 我想将其转换为数值而不是字符串

Object { "Column with spaces 1": 99, "Column with spaces 2": 37  }

It should be enough to convert it to number by adding unary operator + to the value of item, so something like: 通过将一元运算符+添加到item的值来将其转换为数字就足够了,例如:

d3.csv('somecsv.csv', function(data) {
  const cleanData = data.map((d) => ({
     ...d,
     fieldNameWithNumber: +d[fieldNameWithNumber]
  });
});  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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