【问题标题】:d3 localStorage compare arrays .filter coffee script; array variable not workingd3 localStorage 比较数组 .filter 咖啡脚本;数组变量不起作用
【发布时间】:2015-03-02 01:54:12
【问题描述】:

我正在有条件地设置 div 的 CSS。为此,我使用 .filter。过滤器比较 2 个数组。 当我运行下面的代码时,它不起作用。当我硬编码 array_1 内容(取自控制台;在显示的代码中取消注释)时,它确实有效。

我假设循环产生的数组没有及时为过滤函数准备好?

  labelEnter.append("div")
  .attr("class", "bubble-label-name")
  .text((d) -> textValue(d))


  arraylocal = []
  typeofKey = null
  for key of localStorage
    typeofKey = (typeof localStorage[key])
    array = arraylocal.push key
    break
  console.log(array)

 # array = ["show", "cum", "nec", "show"]

  console.log(d3.selectAll(".bubble-label-name").filter((d) -> textValue(d) in array).style("border", "1px solid red"))

为什么使用硬编码的数组内容有效,但是当我使用数组变量时却不行?

【问题讨论】:

    标签: javascript arrays d3.js coffeescript global-variables


    【解决方案1】:

    您似乎希望arrayLocal.push(key) 返回该数组的副本。但是,push() method returns a the new length of the array,这就是将存储在您的array 变量中的全部内容。

    【讨论】:

      【解决方案2】:

      感谢您的回复。以下代码有效。我会查看您发布的链接。谢谢。

       arraylocal = []
        for key of localStorage
          arraylocal.push key
      
      
      d3.selectAll(".bubble-label-name").filter((d) -> textValue(d) in arraylocal).style("border", "1px solid red")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-29
        • 2015-02-13
        • 2013-06-26
        • 2018-01-25
        • 1970-01-01
        相关资源
        最近更新 更多