简体   繁体   English

将数组从视图传递到控制器

[英]Passing an array from the view to a controller

I'm trying to pass an array from a view to a controller. 我试图将数组从视图传递到控制器。 So far I have this: 到目前为止,我有这个:

<form method="post">
    <% for(let i = 0; i < websites.length; i++){ let website = websites[i]; %>
    <fieldset>
        <label for="website<%= i %>" class="col-sm-2">Website <%= i + 1 %></label>
        <input name="website<%= i %>" id="website<%= i %>" value="<%= website %>" type="text"/>
    </fieldset>
    <% } %>
    <button class="btn btn-primary col-sm-offset-2">Generate report</button>
</form>

And this is the controller 这是控制器

const websites = Object.keys(req.body).filter(key => key.startsWith('website'));
console.log(websites);

The problem is the console logs this: 问题是控制台记录以下内容:

[ 'website0', 'website1' ]

So I'm guessing it's grabbing the names. 所以我猜它正在抢名字。 How do I make grab the values instead? 我该如何获取值呢?

An alternative is using the function map 一种替代方法是使用功能map

const websites = Object.keys(req.body)
                       .filter(key => key.startsWith('website'))
                       .map(k => req.body[k]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM