【问题标题】:Pushing elements to an array in Jade将元素推送到 Jade 中的数组
【发布时间】:2013-06-26 18:47:29
【问题描述】:

我正在尝试在 Jade 中创建一个数组,然后遍历另一个数组并从中创建一个新数组。我正在做类似的事情:

         .metadata4
          - var prepopulate = []
          - if (entry.artist_ids)
            - for (var artist in entry.artist_ids)
              - var name = entry.artist_ids[artist]
              - prepopulate.push({id: name, name: name})
        input(type='text', class='token-input', data-url="/query", data-pre=prepopulate)

这一切都很棒。但是,它似乎在列表的末尾添加了一个额外的元素(比如将函数 push 推送到数组或其他东西)。有人知道为什么会这样吗?

【问题讨论】:

    标签: html node.js express pug


    【解决方案1】:

    我不确定为什么会这样,也没有测试过。作为一种解决方法,您可以做的是,声明一个新的var i = 0; 然后在for 中增加它。并将prepopulate.push({id: name, name: name}) 放入if 语句中,例如if(i < entry.artist_ids.length - 1){...push...}

    【讨论】:

    • 我喜欢它:-var array = [] 然后-array.push({time:"22h47"}),谢谢
    【解决方案2】:

    我无法重现此行为,使用以下数据

    entry = { "artist_ids": {1: "hello", 2:"robert"} } 
    

    我得到以下数据-pre:

    [{"id":"hello","name":"hello"},{"id":"robert","name":"robert"}]
    

    这正是你写的(虽然我怀疑你想用艺术家替换 id 的名称值)

    也许您可以发布您的条目对象?

    【讨论】:

      猜你喜欢
      • 2018-09-19
      • 2013-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-16
      • 1970-01-01
      相关资源
      最近更新 更多