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
{ "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()
});
Answer from PineCone on Stack Overflowjavascript - How to create an empty array of a given size? - Stack Overflow
Add new value to an existing array in JavaScript - Stack Overflow
Insert items in a blank array
How to initialize empty array of objects?
1) To create new array which, you cannot iterate over, you can use array constructor:
Array(100) or new Array(100)
2) You can create new array, which can be iterated over like below:
a) All JavaScript versions
- Array.apply:
Array.apply(null, Array(100))
b) From ES6 JavaScript version
- Destructuring operator:
[...Array(100)] - Array.prototype.fill
Array(100).fill(undefined) - Array.from
Array.from({ length: 100 })
You can map over these arrays like below.
Array(4).fill(null).map((u, i) => i)[0, 1, 2, 3][...Array(4)].map((u, i) => i)[0, 1, 2, 3]Array.apply(null, Array(4)).map((u, i) => i)[0, 1, 2, 3]Array.from({ length: 4 }).map((u, i) => i)[0, 1, 2, 3]
var arr = new Array(5);
console.log(arr.length) // 5
You don't need jQuery for that. Use regular javascript
var arr = new Array();
// or var arr = [];
arr.push('value1');
arr.push('value2');
Note: In javascript, you can also use Objects as Arrays, but still have access to the Array prototypes. This makes the object behave like an array:
var obj = new Object();
Array.prototype.push.call(obj, 'value');
will create an object that looks like:
{
0: 'value',
length: 1
}
You can access the vaules just like a normal array f.ex obj[0].
This has nothing to do with jQuery, just JavaScript in general.
To create an array in JavaScript:
var a = [];
Or:
var a = ['value1', 'value2', 'value3'];
To append values on the end of existing array:
a.push('value4');
To create a new array, you should really use [] instead of new Array() for the following reasons:
new Array(1, 2)is equivalent to[1, 2], butnew Array(1)is not equivalent to[1]. Rather the latter is closer to[undefined], since a single integer argument to theArrayconstructor indicates the desired array length.Array, just like any other built-in JavaScript class, is not a keyword. Therefore, someone could easily defineArrayin your code to do something other than construct an array.