D3:使用一元加号将字符串转换为数字无效。

huangapple go评论59阅读模式
英文:

D3: converting from string to number with unary + sign is not working

问题

以下是翻译好的部分:

"我有一个CSV文件,我已经导入了。导入后,我使用嵌套(nest)和汇总(rollup)来组织数据,如下所示。

      var data_group = d3.nest()
                      .key(function(d) {return +d.year;})
                      .key(function(d) {return +d.average_rating;})
                      .rollup(function (count) {
                        return count.length;
                      })
                      .entries(data);

      console.log(data_group) 

然而,当我查看console.log时,年份和average_rating都是字符串。我希望它们是数字。如何实现这一点?下面是输出的图片。此外,有人可以解释一下为什么在加号(+)之后它们不是数字吗?"

英文:

I have a CSV file that I have imported.
after import, I have used nest and rollup to organize the data like below.

      var data_group = d3.nest()
                      .key(function(d) {return +d.year;})
                      .key(function(d) {return +d.average_rating;})
                      .rollup(function (count) {
                        return count.length;
                      })
                      .entries(data);

      console.log(data_group) 

However, when I look at the console.log, the year and the the average_rating are strings.
I would like them to be a number.
How can I achieve this? Below is a picture of the output.
Also, can someone explain why they are not numbers after the + sign?
D3:使用一元加号将字符串转换为数字无效。

答案1

得分: 1

这正如预期的那样,就像在d3.nest文档中描述的一样。它还在nest.key的文档中明确指出,“键函数...必须返回一个字符串标识符”。

那里给出的示例的前几行如下所示:

[{key: "1931", values: [
   {key: "Manchuria", values: [
   {yield: 27.00, variety: "Manchuria", year: 1931, site: "University Farm"},
   {yield: 48.87, variety: "Manchuria", year: 1931, site: "Waseca"},
...

特别要注意的是,第一行以 key: "1931" 开始,即使 1931 在输入数据中是一个整数值。只有在它作为值出现时,例如在最后两行中,它才会以整数形式出现在结果中。

值得一提的是,d3.nest 已经被废弃很久了,甚至在几年前的 V6 版本中就已经移除了。你应该认真考虑在 V7 中使用 d3.group 和/或 d3.rollup

英文:

This is exactly as expected, as described in the d3.nest documentation. It also states explicitly in the documentation of nest.key that "The key function ... must return a string identifier".

The first few lines of the example given there look like so:

[{key: "1931", values: [
   {key: "Manchuria", values: [
   {yield: 27.00, variety: "Manchuria", year: 1931, site: "University Farm"},
   {yield: 48.87, variety: "Manchuria", year: 1931, site: "Waseca"},
...

In particular, note that the first line starts key: "1931", even though 1931 is an integer value in the input data. That 1931 appears as an integer in the result only when it's a value, as in the lat two lines.

It's probably also worth pointing out that d3.nest is long since deprecated and was even removed in V6 several years ago. You should seriously consider using d3.group and/or d3.rollup in V7.

huangapple
  • 本文由 发表于 2023年2月18日 11:08:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定