我需要使用什么语法来向一个对象的成员添加一个临时数组,该成员等同于一个字符串的通用列表

What syntax do I need to use to add a temporary array to a member of an object that equates to a generic list of string?

本文关键字:成员 一个 数组 等同于 列表 字符串 语法 一个对象 什么 添加      更新时间:2023-09-26

根据这里的答案,我想我可以这样声明并分配给数组:

var _recipients = [];
    if ($('#email1').length > 0) {
        _recipients.push($('#email1').val());
    }
    if ($('#email2').length > 0) {
        _recipients.push($('#email3').val());
    }
    if ($('#email3').length > 0) {
        _recipients.push($('#email3').val());
    }

然后尝试将该数组添加到像这样的对象的成员中:

var saveConfigModel = {
    unit: $('#unitsselect').val(),
    scheduleProduceUsage: $('#ckbx_produceusage').is(':checked'),
    scheduleDeliveryPerformance: $('#ckbx_deliveryperformance').is(':checked'),
    scheduleFillRate: $('#ckbx_fillratebycustomer_location').is('checked'),
    schedulePriceCompliance: $('#ckbx_pricecompliance').is('checked'),
    recipients.push(_recipients),
    . . .

但是JSLint抱怨最后一行代码,说:"期望的是‘:’,而看到的是‘.’。"

在更多的上下文中,最后一个是AJAX调用的一部分:

var saveConfigModel = {
    unit: $('#unitsselect').val(),
    scheduleProduceUsage: $('#ckbx_produceusage').is(':checked'),
    scheduleDeliveryPerformance: $('#ckbx_deliveryperformance').is(':checked'),
    scheduleFillRate: $('#ckbx_fillratebycustomer_location').is('checked'),
    schedulePriceCompliance: $('#ckbx_pricecompliance').is('checked'),
    recipients.push(_recipients),
        generationDayOfMonth: $('#dayofmonthselect').val(),
        generationOrdinal: $('#ordinalselect').val(),
        generationDayOfWeek: $('#dayofweekselect').val(),
        generationWeekOrMonth: $('#weekormonthselect').val(),
        daterangeFromProduceUsage: $('#produsagefrom').val(),
        daterangeToProduceUsage: $('#produsageto').val(),
        daterangeFromDeliveryPerformance: $('#delperffrom').val(),
        daterangeToDeliveryPerformance: $('#delperfto').val(),
        daterangeFromFillRate: $('#fillratefrom').val(),
        daterangeToFillRate: $('#fillrateto').val(),
        daterangeFromPriceCompliance: $('#pricecompliancefrom').val(),
        daterangeToPriceCompliance: $('#pricecomplianceto').val()
    };
    $.ajax({
        type: "POST",
        url: '@Url.Action("PostUnitConfig", "SaveConfig")',
        async: true,
        contentType: 'application/json',
        dataType: "json",
        data: JSON.stringify({ model: saveConfigModel })
    });

当我只是给"收件人"添加一个值时,它运行得很好:

recipients: $('#email1').val(),

在服务器上的模型中,收件人是一个字符串的通用列表:

public class UnitConfigVals
{
    . . .
    public List<string> recipients { get; set; }
    . . .

我需要修复什么才能使此数组分配工作?

更改

recipients.push(_recipients),

recipients: _recipients

您当前正在调用函数而不是进行赋值。

旁注:你可以用替换你的第一个代码块

var _recipients = $('[id^="email"]').map(function () {
    return $(this).val();
}).get().filter(function (y) {
    return y != '';
});