小编Jam*_*een的帖子

在d3.js折线图中添加图例

我使用https://bl.ocks.org/mbostock/3884955中的代码绘制多系列折线图.

线条末端有标签,但如果线条彼此靠近则会有点乱.

是否可以使用d3将图例添加到折线图?我查看了API,但似乎找不到任何东西.

javascript linechart legend d3.js

0
推荐指数
1
解决办法
3572
查看次数

Change Array.prototype in node.js

I want to make some functions available to all my arrays.

For instance, I want a function to remove duplicates:

Array.prototype.uniq = function () {
  return Array.from(new Set(this));
};
Run Code Online (Sandbox Code Playgroud)

But I want to make this function work in my entire node.js project.

Will it work if I just put it in server.js which is run when I type npm start?

It would be great if it also works on the client. Is it possible or should I consider server …

javascript arrays prototypejs node.js

0
推荐指数
1
解决办法
2038
查看次数

根据以前的值来浏览数组

我有一个阵列['green', 'red', 'yellow', 'blue'].

我想用这些颜色改变我网站的背景颜色.但我不想从这个数组中随机抽取颜色.我想按顺序迭代它.

因此,如果我得到我的网站的背景颜色getBgColor()并打印red,那么我想要一个功能setBgColor(currentColor)打印yellow.我该怎么做呢?

我想我应该做点什么

function setBgColor(currentColor) {
  var array = ['green', 'red', 'yellow', 'blue'];

  newColor = array[array.indexOf(currentColor) + 1];
}
Run Code Online (Sandbox Code Playgroud)

但这是正确的方法吗?我如何确保从蓝色变为绿色,从而不超过阵列的长度?

javascript arrays

0
推荐指数
1
解决办法
33
查看次数

在 JavaScript 中返回一个带有一些额外属性的新对象

我有一个对象

usersById: {
  1: { name: 'John' },
  2: { name: 'Michelle' },
  ...
}
Run Code Online (Sandbox Code Playgroud)

我想返回相同的对象,但首先使用新属性填充 id=2 处的对象age,但坚持不变性。

我猜它会是这样的

return {
  ...usersById,
  ...usersById[2].age = 40
}
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误In this environment the sources for assign MUST be an object. This error is a performance optimization and not spec compliant

或者,我猜它应该是这样的

return Object.keys(usersById).map(userId => {
  if (userId === 2) {
    return {
      ...usersById[2],
      ...age = 40
    }
  }
  return usersById[userId]
})
Run Code Online (Sandbox Code Playgroud)

但它返回一个数组而不是一个对象。

javascript arrays object immutability reducers

0
推荐指数
1
解决办法
2701
查看次数