【问题标题】:index array with .map doesn't work on nodejs带有.map的索引数组在nodejs上不起作用
【发布时间】:2018-08-16 17:32:11
【问题描述】:

大家好,我尝试使用映射 json 数组进行索引。但它不能正常工作。我认为我的代码不正确。

我的代码:

var  entireHTMLssssq = lloopmois.map(result, index => `<div id=${index}  style="position: absolute;">  <b style="position:absolute;top:-6px;left:2px"> ${result} </b>    </div>`).join('')

你能告诉我正确的语法吗?

【问题讨论】:

  • 什么是lloopmois? result 是什么?
  • 您需要提供minimal reproducible example(包括示例输入,使用问题编辑器中的实时演示功能)和明确的问题陈述(“无法正常工作"告诉我们很少,告诉我们你得到的和你期望的有什么区别。
  • @Jeto 是 json 数组。
  • 缩进代码以获得更好的可读性
  • @mataka — JSON 数组?所以var lloopmois = "[1,2,3]";?您需要先解析 JSON 以将其转换为 JavaScript 数组,然后才能对其调用 map。

标签: javascript arrays json node.js


【解决方案1】:

如果您有多个参数,则需要使用 arrow functions 将参数括起来。

lloopmois.map((result, index) => ...)
//            ^             ^

【讨论】:

    【解决方案2】:

    问题在于括号 () 和 return 用于地图功能。

    array.map((result, index)=&gt;{return ...}) 是正确的语法

    var  entireHTMLssssq = lloopmois.map((result, index) => {
    return `<div id=${index} style="position: absolute;">  <b 
    style="position:absolute;top:-6px;left:2px"> ${result} </b>    
    </div>`}).join('')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 2017-12-13
      • 2010-12-09
      相关资源
      最近更新 更多