【问题标题】:My EJS's codes is in a string and I can't add array index. How can I add an index number?我的 EJS 的代码在一个字符串中,我无法添加数组索引。如何添加索引号?
【发布时间】:2019-11-29 17:10:00
【问题描述】:

第一个和第二个代码不运行。但是,当我写一个数字时,第三个代码会运行。我为'number'写一个值,例如这样,var number = 1;.

这个代码我试过了,

alert("<%- graphic[0].graphics_objects[' + number + ']._id -%>");

但它没有..我怎样才能在里面写一个索引号?

第一个代码:

alert("<%- graphic[0].graphics_objects[number]._id -%>");

第二个代码:

alert("<%- graphic[0].graphics_objects[<%=number%>]._id -%>");

第三个代码:

alert("<%- graphic[0].graphics_objects[1]._id -%>");

【问题讨论】:

  • alert("&lt;%= graphic[0].graphics_objects[0]._id %&gt;"); 当我用'
  • 我们不知道您的 graphic 对象/数组是什么样子或 number 来自哪里。提供minimal reproducible example

标签: javascript arrays node.js string ejs


【解决方案1】:

这是我的图形模型,


var graphicSchema = new mongoose.Schema({
    project_id: {
        type: String,
        ref: 'Project',
        required: true
    },
    title: {
        type: String
    }
    graphics_objects: [{
        point_pool_id: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Point_pool'
            //required: true
        }
        value: {
            type: String
        }
    }]
});

module.exports = restful.model('Graphic', graphicSchema);

这是 project.ejs


    for (var number= 0; number< '<%-graphic[0].graphics_objects.length-%>'; number++) {
        go_id = '<%- graphic[0].graphics_objects[number]._id -%>';

    }

【讨论】:

  • 这个for循环在脚本标签中
猜你喜欢
  • 2023-04-01
  • 2021-06-20
  • 2018-10-08
  • 2012-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
相关资源
最近更新 更多