Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何在ejs模板中使用in_数组_Node.js_Express_Ejs - Fatal编程技术网

Node.js 如何在ejs模板中使用in_数组

Node.js 如何在ejs模板中使用in_数组,node.js,express,ejs,Node.js,Express,Ejs,如何在ejs模板中使用in_array或类似工具 以下是我在php上使用它的方式: <?php $cur_repository_id = array(); foreach($dbrepos as $row){ $shared_ids = $row['shared_ids']; if(in_array($user_id, $shared_ids)){ $selected = "selected"; $cur_repository_id[] =

如何在ejs模板中使用in_array或类似工具

以下是我在php上使用它的方式:

<?php
$cur_repository_id = array();
foreach($dbrepos as $row){
    $shared_ids = $row['shared_ids'];
    if(in_array($user_id, $shared_ids)){
        $selected = "selected";
        $cur_repository_id[] = $row['rep_id'];
    }else{
        $selected = "";
    }
?>
<option value="<?php echo $row['rep_id'];?>" <?php echo $selected;?>><?php echo $row['namespace'];?></option>
<?php
}
?>

相当于javascript中数组中的

因此,例如,如果您想将数组从
Node.js
传递到
ejs
文件,您可以这样做:

res.render('index', {numbers : [1, 23, 44]});
然后在
ejs
中,您可以使用方法
includes
,例如:

<body>
    <% if(numbers.includes(44)){ %>
        <h1>44 exists</h1>
    <% } else{ %>
        <h1>44 not exists</h1>
    <% } %>
</body>

44存在
44不存在

您还可以使用方法,该方法返回数组中给定值的索引。

因此,它不是一种代码编写服务,您必须展示您迄今为止所做的尝试。