admin管理员组文章数量:1316980
Controller sends a JSON for one Frame. I need to keep incrementing the array where Multiple Frame's score gets added. For example
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!!!");
}
});
});
Controller sends a JSON for one Frame. I need to keep incrementing the array where Multiple Frame's score gets added. For example
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!!!");
}
});
});
Share
Improve this question
edited Jan 31, 2016 at 16:31
rrk
15.9k4 gold badges30 silver badges47 bronze badges
asked Jan 31, 2016 at 16:19
PineConePineCone
2,35313 gold badges42 silver badges88 bronze badges
9
-
"Issue The values do not get added and shows data for each Frame only." Not certain interpret Question correctly . What is purpose of calling
bowlingData.frames.push(temp)
twice?, withinfor
loop , and afterfor
loop ? Can create jsfiddle jsfiddle to demonstrate ? – guest271314 Commented Jan 31, 2016 at 16:25 - jsfiddle/xycL97a4/1 It was mistake. I was trying to add two entries toegther. When I print I can see arrays are getting added but final result prints out only current input. Not the total of old input + curent input. – PineCone Commented Jan 31, 2016 at 16:39
- See doc.jsfiddle/use/echo.html – guest271314 Commented Jan 31, 2016 at 16:40
- "It was mistake. I was trying to add two entries toegther. When I print I can see arrays are getting added but final result prints out only current input. Not the total of old input + curent input. " Is Question resolved ? – guest271314 Commented Jan 31, 2016 at 16:42
- 1 @guest271314 the issue is resolved. Thanks for checking the question. – PineCone Commented Feb 1, 2016 at 21:49
1 Answer
Reset to default 4Apparently 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
{ "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 (i.e. frames) and on the 3rd line the value was pused to the empty Array
. Although the the for loop was adding each element to the Array
(i.e. 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. So no need to create any Array
to increment multiple input result. 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()
});
本文标签: jqueryHow to add items to an empty array in JavascriptStack Overflow
版权声明:本文标题:jquery - How to add items to an empty array in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742001277a2411060.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论