简体   繁体   English

如何在JavaScript中将项目添加到空数组

[英]How to add items to an empty array in Javascript

Controller sends a JSON for one Frame. 控制器发送一帧的JSON。 I need to keep incrementing the array where Multiple Frame's score gets added. 我需要不断增加添加多帧得分的数组。 For example 例如

FrameScore = f1 + f2 +.. lastF FrameScore = f1 + f2 + .. lastF

Issue The values do not get added and shows data for each Frame only. 问题值未添加,仅显示每个帧的数据。 Where am I doing it wrong? 我在哪里做错了?

var bowlingData = {
    "frames": []
};
var frames = [];
$('#submitButton').click(function(e) {
    frames.push([$("#FirstRoll").val(), $("#SecondRoll").val()]);
    for (var ln = 0; ln < frames.length; ln++) {
        var temp = {
            "firstroll": $("#FirstRoll").val(),
            "secondroll": $("#SecondRoll").val()
        };
        bowlingData.frames.push(temp);
    }
    console.log("temp data: " + temp);
    bowlingData.frames.push(temp);
    var element = this;
    $.ajax({
        url: "/Home/Submit",
        type: "POST",
        data: JSON.stringify(bowlingData),
        dataType: "json",
        traditional: true,
        contentType: "application/json; charset=utf-8",
        success: function(data) {
            var parseData = JSON.parse(data);
            console.log("MyDate: " + data.score);
            console.log("Parse" + parseData);
            $("#lblTotalScore").text(parseData.score);
            $("#FirstRoll").val("");
            $("#SecondRoll").val("");
        },
        error: function() {
            alert("An error has occured!!!");
        }
    });
});

Apparently the solution is 显然解决方案是

var frameArray= [];
frameArray.push("#FirstRoll");
frameArray.push("#SecondRoll");

But that is to add single elements to the array. 但这就是将单个元素添加到数组中。 If the input is [[2, 3],[4, 5],...] then the JSON object representation would be 如果输入为[[2,3],[4,5],...],则JSON对象表示为

{ "frames": [{"first": 2, "second": 3}, {"first": 4, "second": 5}, ... ] } 

However, there was another issue of not getting the correct response from the controller. 但是,还有另一个问题,就是无法从控制器获得正确的响应。

The issue here is that an empty array is created (ie frames) and on the 3rd line the value was pused to the empty Array . 这里的问题是创建了一个空数组(即帧),并且在第三行将值推到了空Array Although the the for loop was adding each element to the Array (ie frames) but when the response was created the recent input was replacing the previous input, because the JSON object bowlingData was holding temp data only. 尽管for循环将每个元素添加到Array (即帧),但是在创建响应时,最近的输入将替换先前的输入,因为JSON对象bowlingData仅保存临时数据。 So no need to create any Array to increment multiple input result. 因此,无需创建任何Array即可递增多个输入结果。 Initial value would be hold by the browser and second input would be added in next submit. 初始值将由浏览器保留,第二个输入将添加到下一个提交中。

Was

var frames = [];
$('#submitButton').click(function(e) {
frames.push([$("#FirstRoll").val(), $("#SecondRoll").val()]);
for (var ln = 0; ln < frames.length; ln++) {
    var temp = {
        "firstroll": $("#FirstRoll").val(),
        "secondroll": $("#SecondRoll").val()
    };
    bowlingData.frames.push(temp);
}

Should be 应该

$('#submitButton').click(function (e) {

bowlingData.frames.push({
   "firstroll": $("#FirstRoll").val(),
   "secondroll": $("#SecondRoll").val()
});

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

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