Populate specific array indices from other array in JavaScript - javascript

I have 2 arrays, arr1 and arr2. They're both 2-dimensional. I want to copy certain array values from arr1 to arr2.
For instance, I want to copy the value from arr1[9][9] into arr2[0][0]. My guess was to write arr2[0][0] = arr1[9][9]; but that failed.
I looked at some similar questions on this site but they did not answer my question.
Here is the code for the particular situation. The code is written is Google Apps script.
// eplList and attList are both arrays. They are filled below (I checked, the values exist)
var eplList = epl.getRange(2, 2, eplLastRow, 2).getValues();
var attList = attsheet.getRange(3, 1, attLastRow, 20).getValues();
var eplListLength = eplList.filter(String).length;
var attListLength = attList.filter(String).length;
// Declaring the empty array I want to fill
var masterArray = [];
var ix, jx, day;
// Here I begin to fill the array
for (ix = 0; ix < eplListLength; ix++)
{
masterArray[ix][0] = eplList[ix][0]; // This is where I am getting the error message
masterArray[ix][1] = eplList[ix][1];
for (jx = 0; jx < attListLength; jx++)
{
if (eplList[ix][0] == attList[jx][day*4-4])
masterArray[ix][6+day] = masterArray[ix][6+day].concat(" ", attList[jx][day*4-2], ": ",attList[jx][day*4-3]);
};
// and some morecode
};
The error I'm getting is "TypeError: Cannot set property '0' of undefined"

To fix particular issue you've got try this code, but i not sure about code below):
masterArray[ix] = [eplList[ix][0], eplList[ix][1]]; // This is where I am getting the error message

Related

Issue with object property '0' of undefined

My code yields the following error:
Uncaught TypeError: Cannot set property '0' of undefined
First, here's the screenshot of the table
Note:
This table are the assigned work schedule of the student.
Let's proceed to my code:
function saveWorkSched(){
// listWorkSched
var arr=[];
var getSAWorkSched=[[],[]];
var wsCounter=0;
var wsCounter2=0;
var j = 0;
$("#listWorkSched td").each(function(){
arr.push($(this).text());
});
console.log(arr);
for(j;j<arr.length;j++){
if(wsCounter2<=2){
getSAWorkSched[wsCounter][wsCounter2]=arr[j];
wsCounter2++;
}else{
wsCounter++;
wsCounter2=0;
getSAWorkSched[wsCounter][wsCounter2]=arr[j];
wsCounter2++;
}
}
}
1st phase:
after the user create the work schedule this will be stored in arr variable.
2nd phase:
the arr value will converted to multi-dimensional array and will be stored in getSAWorkSched variable
after the 3rd loop an error will occurred. it means that every time I create a work schedule more than 2 the error will trigger.
else{
wsCounter++;
wsCounter2=0;
getSAWorkSched[wsCounter][wsCounter2]=arr[j]; // Here's the code where the error specified based on the console of my browser
wsCounter2++;
}
You need to define the nested array that you try to access. It really comes down to the same principle: you address the following:
getSAWorkSched[wsCounter][wsCounter2]
... with a wsCounter value that is eventually getting to 2, but you have only defined two nested arrays in the initialisation of getSAWorkSched, so getSAWorkSched[2] does not exist -- it will give you undefined. Trying to get an array element from nothing (undefined) is not possible. So add this line before it in the else bock:
getSAWorkSched[wsCounter] = []; // <--- Add this
getSAWorkSched[wsCounter][wsCounter2]=arr[j];
More elegant code
You could use $.map and slice to write this in a more elegant way:
function saveWorkSched() {
var arr = $.map($("#listWorkSched td"), function (td) {
return $(td).text();
});
var getSAWorkSched = [];
for (var j = 0; j < arr.length; j += 3) {
getSAWorkSched.push(arr.slice(j, j + 3));
}
}

Google Script (Forms) - Cannot Convert Array to Choice[]

I have looking into this issue for a while, but have yet to find a suitable answer (most involve switching to setChoiceValues() rather than addressing the "Cannot convert Array to Choice[]" issue with setChoices([])).
While attempting to generate form sections and questions via Google Script, I ran into the issue of not getting my answer selections to go to specific pages based on the user's answer. This appears to be the difference between setChoiceValues() and setChoices([]), with the latter allowing for page navigation as best as I can tell.
However, when attempting to put my array of new choices into setChoices([]), I get the error message "Cannot convert Array to Choice[]". My code works fine otherwise, but I need to use setChoices([]) (it seems) in order to get the page navigation that I want.
How can I loop values into an array or other container and be able to make them appear as a Choices[] object? How can I make something like this work? It seems like it should be much easier than it is, but I cannot see the solution.
Below is a segment of my code that is causing the issue:
//Form - globally accessible
var f = FormApp.openById(f_id);
//Date Iterator
var curr_date = 0;
//Time Iterator
var curr_time = 0;
//Array of Times
var Tchoices = [];
//Setting Time choices per date
while(curr_date < dates.length)
{
Tchoices = [];
curr_time = 0;
//dates is an array of objects with both d's (single date) and t's
// (array of times for that date)
var d = dates[curr_date].d;
var end_break = f.addPageBreakItem().setTitle("Times for " + d);
var f_time = f.addMultipleChoiceItem().setTitle(d);
while(curr_time < dates[curr_date].t.length)
{
end_break = end_break.setGoToPage(FormApp.PageNavigationType.SUBMIT);
Tchoices.push(f_time.createChoice(dates[curr_date].t[curr_time], end_break).getValue());
curr_time++;
}
f_time.setChoices([Tchoices]);
}
There was some minor issues with the building of your MultipleChoise object:
//Form - globally accessible
var f = FormApp.openById('someID');
//Date Iterator
var curr_date = 0;
//Time Iterator
var curr_time = 0;
//Array of Times
var Tchoices = [];
//Setting Time choices per date
while(curr_date < dates.length)
{
Tchoices = [];
curr_time = 0;
//dates is an array of objects with both d's (single date) and t's
// (array of times for that date)
var d = dates[curr_date].d;
var end_break = f.addPageBreakItem().setTitle("Times for " + d);
var f_time = f.addMultipleChoiceItem();
f.addMultipleChoiceItem().setTitle(d);
while(curr_time < dates[curr_date].t.length) //verify this while not sure what you have inside your dates array
{
end_break = end_break.setGoToPage(FormApp.PageNavigationType.SUBMIT);
Tchoices.push(f_time.createChoice(dates[curr_date].t[curr_time])); //You cannot add a pagebreak inside the elements of a choiseItem array
curr_time++;
}
Logger.log(Tchoices);
f_time.setChoices(Tchoices);
}
Check the values for dates[curr_date].t.length inside the Loop I'm not sure how you constructed the array.
You cannot add a pagebreak inside the elements of a choiseItem array

Make a 2d array with 2 simple array in javascript

I've been searching for hours for an error in my javascript's code but I really don't understand why this error occur.
So I have 2 array that I've get by using ajax and I want to merge them into an 2d array but this error happen :
Uncaught TypeError: Cannot set property '0' of undefined
So this is my code :
var arrayCityAccident = new Array([]);
for(var i = 0; i < responseAccident.length; i++)
{
arrayCityAccident [i][0] = responseCity[i]['city'];
arrayCityAccident [i][1] = responseAccident[i];
}
I've look up to see if my both 1d array have values and yes they have values so if someone could help me it will help me a lot.
Thank you in advance !
You need to add a new array to arrayCityAccident for each index i:
var arrayCityAccident = [];
for(var i = 0; i < responseAccident.length; i++)
{
arrayCityAccident.push([responseCity[i]['city'], responseAccident[i]]);
}
Well, as soon as i becomes bigger than 0 in your loop, arrayCityAccident[i] doesn't return an array anymore. You only defined arrayCityAccident[0], so accessing arrayCityAccident[i][0] isn't possible.
Just add another array to arrayCityAccident before defining its elements:
var arrayCityAccident = new Array([]);
for(var i = 0; i < responseAccident.length; i++)
{
arrayCityAccident[i] = []; // add a new array to arrayAccident
arrayCityAccident[i][0] = responseCity[i]['city']; // now you can set those properties
arrayCityAccident[i][1] = responseAccident[i]; // without problems
}

number of xml attributes->javascript multi array->function / process

I have comments in a database relative to each post. It pulls the post and all comments according to that post in one query, and groups them inside an XML node. I get the amount of attributes in each node, and take away the standard number of attributes that every post has by default, and that leaves me with the number of comments.
The comment structure is as follows:
comment0 Hey nice post!
commentdate0 2014-12-1 08:25:02
commentaudthor0 Chris
comment1 cool!
commentdate1 2014-08-2 09:25:02
commentaudthor1 Jason
and so on, the comments increase by that number.
So I need to check how many comments there are (done) and then retrieve them from the xml node (using $(this).attr('comment'+i)) Where i would be the counter (comment0, comment1 and so on)
Here is my current code to get it into the array:
var comms = new Array();
var count = this.attributes.length;
var av = count-11;
if(av != 0) {
for(var i=0; i<av; i++) {
for(var j=0; j<2; j++){
comms[i][j] = $(this).attr('comment'+i);
comms[i][j+1] = $(this).attr('commentdate'+i);
comms[i][j+2] = $(this).attr('commentauthor'+i);
}
}
}
But it is giving me the following error:
Uncaught TypeError: Cannot set property '0' of undefined
Now, how can I load it into a multi dimensional array to store the data, pass it to a function, and then process each row separately?
ie: this is what I am trying to do:
Array {
'comment1':
comment
commentdate
commentauthor
'comment2':
comment
commentdate
commentauthor
}
and then how would I be able to process each comment inside the function? ie: with each comment, do this.
Thanks in advance!
You need to create the inner array before adding to it. Try this:
var comms = new Array();
var count = this.attributes.length;
var av = count-11;
//if(av != 0) { // I commented this condition out, as it is not needed here
for(var i=0; i<av; i++) {
comms[i] = []; // Create a new array here before adding to it (this syntax is more common than the longer "new Array()" syntax you used
comms[i][0] = $(this).attr('comment'+i);
comms[i][1] = $(this).attr('commentdate'+i);
comms[i][2] = $(this).attr('commentauthor'+i);
}
//}

Cannot access second field in a 2 dimension array in javascript

I have a 2 dimension array defined as
var thischart = [[],[]];
and the array contains the following data created programatically:
1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0,10,0,11,0,12,0,13,24,14,0,15,0,16,0,17,0,18,0,19,0,20,0,21,0,22,0,23,0,24,0
I cannot get the single value of the second field in the particular array cell. For example, if I use the following command to get the value:
alert("thischart[i,1]=" + thischart[0, 1]);
I get the following answer:
thischart[i,1]=2,0
I tried using the second dimension to access the data as
thischart[0][1]);
but it gives me an error message:
I just want to get the second single value in the array such as for array cell 13 I want the value 24 from above. Does anyone have an answer on how to access this array?
I populated the array as follows and then updated it thru program logic:
$hours = [];
for($i = 0; $i< 24; $i++){
$hours[$i] = [];
$hours[$i][0] = ($i + 1);
$hours[$i][1] = "0";
}
And the answer to this question is below:
for(var i in thischart){
var tc = thischart[i];
myvalue = tc[1]); // this is the value I want
}
Thanks to everyone who responded.
For all of them like this:
for(var i in thischart){
var tc = thischart[i];
for(var n in tc){
// n is internal index
// tc[n] is internal value
}
}
For a single value from the first internal Array, the second value:
thischart[0][1];
Why don't you use the console to see what's the return of
thischart[0];
Because it should contain an array. If it does, then
thischart[0][1];
is perfectly valid syntax. And if it doesn't, then
thischart[0,1]
means nothing whatsoever.
Do something like this:
var items = [[1,2],[3,4],[5,6]];
alert(items[0][0]); // 1
do you mean something like this:...
http://jsfiddle.net/DSrcz/1/
var arr = [1,0,0,0,0,0,0,0,0,0,0,0,0,24,0,0,0,0];
arr[33]=1000;
alert(arr[13]);
alert(arr[33]);

Categories