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));
}
}
Related
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
I am using p5 loadJSON to import data, the data gives an array with so many records and sub-arrays.
For example, data.records[i].form_values["94d5"] gives me a name.
I am currently using a for loop to go through data.records.length and give me an array of some key pieces of data relative to each record I want to see.
Problem:
I want to loop through the records and get the value of:
data.records[i].form_values["94d5"].choice_values["0"], but some records don't have form_values["94d5"], for example. I just want to skip empty sections (leave undefined like empty items) insted i get an error that choice_values is undefined.
My loop is:
function gotData(data){
var i = 0;
var statusFind = data.records[i].status;
for(i = 0; i < data.records.length; i++) {
var returned = [data.records[i].form_values["6f71"], data.records[i].form_values.b059, data.records[i].form_values["94d5"], data.records[i].form_values.b62d, data.records[i].form_values["3493"].choice_values["0"], data.records[i].status, data.records[i].form_values.af80];
for (j = 0; j < returned.length; returned++){
if (returned[j] == searchKey) {
var result = [returned[0], returned[1], returned[2], returned[3], returned[4], returned[5], returned[6]];
array.push(result);
write();
}
}
}
}
Error Message:
TypeError: Cannot read property "length" from undefined.
Sample Data:
Data
The error message appear after highlighted the sample data and clicked the "Button" that has an Assign script on it. Script is:
function result(range) {
var output2 = [];
for(var i=0, iLen=range.length; i<iLen; i++) {
var s = range[i][1].split(" ");
for(var j=0, jLen=s.length; j<jLen; j++) {
var output1 = [];
for(var k=0, kLen=range[0].length; k<kLen; k++) {
if(k == 1) {
output1.push(s[j]);
} else {
output1.push(range[i][k]);
}
}
output2.push(output1);
}
}
return output2;
}
(saw this code online and trying to make it work, but failed to)
Simply put the error indicates that you have an undefined array.
Looking at your code there are 2 places this could happen. Please check that the following are true:
range is an array passed on to the function (the function cannot be called on its own, somewhere else you must have result(arrayVar) calling this function)
range variable is an array that contains arrays, so it must be at the very least [[]] (for example var range = [[a1,a2,a3],[b1,b2,b3]]) would produce range.lenght = 2 and range[0].lenght = 3, however if you have [a1, a2, b1, b2], then range.lenght = 4 and range[0].lenght will not work
I'm using this code to add values to local storage:
rowSettings[counter] = [value1, value2, value3];
localStorage.setItem("rowSettings", JSON.stringify(rowSettings));
The result in the localStorage is then:
[null,[1,0,0],[1,0,0],[1,0,0],[1,0,0]].
So how come "null" is being added?
In Chome developer tool I get:
"Uncaught TypeError: Cannot read property '0' of null".
This happens when retrieving the objects back:
var row =0;
var retrievedObject = JSON.parse(localStorage.getItem('rowSettings'));
//Start each function
for (var i = 0; i < 3; i++) {
console.log(retrievedObject[row][i]);
}
row++;
//End each function
When you built rowSettings, you started (counter) at index 1 instead of 0. So the 0th element is null.
Answer found based on Jonathan's comment.
I'm trying to parse something like this
{
"popular result":[
{"term":"Summer","url":"http://summer.com"},
{"term":"Autumn","url":"http://autumn.com"},
{"term":"spring","url":"http://spring.com/"},
{"term":"winter","url":"http://winter.com/"}]
}
<script type="text/javascript">
$(document).ready(function () {
$.getJSON('/Controls/GetPopularSearches', function (json) {
for (var i = 0; i < json.length; i++) {
$.each(myJson.i, function (key, value) {
alert(value.term);
});
}
});
});
</script>
but nothing happened! Because is array in array! Please let me know how to do this
Arrays and objects are different things. You will want to investigate them tons more before things get really challenging.
Assuming json really does equal the object you provide (in JSON those show up as {}), then json['popular result'] (you could use a . if there wasn't a space) is the array (in JSON those show up at []) you want to traverse.
For some reason, this confusion got you looping over an object (not going to get you anywhere as length is rarely defined for it) and then (ignoring the typo on myJson), you started looping over something that didn't exist (which didn't crash b/c it never got there).
Cleaning it up...
<script type="text/javascript">
$(document).ready(function () {
$.getJSON('/Controls/GetPopularSearches', function (json) {
for (var i=0;i<json['popular result'].length;i++) {
alert(json['popular result'][i].term + ' points to the URL ' + json['popular result'][i].url);
}
});
});
</script>
Notice how the alert references the json object (that's your variable name), the popular result array, then [i] is the "row" in that array, and the term/url element of the object on that row.
NOTE: Running something with a ton of alerts as you're debugging is annoying. Check out console.log.
You don't need $.each and you need to loop over the array set as the value of popular result which is inside a containing object.
$.getJSON('/Controls/GetPopularSearches', function (json) {
var arr = json['popular result'];
for (var i = 0, l = arr.length; i < l; i++) {
console.log(arr[i].term);
}
});
Demo.
Check this fiddle
var jsontext =
'{"popularresult":[{"term":"Summer","url":"http://summer.com"},{"term":"Summer","url":"http://summer.com"}]}';
var getContact = JSON.parse(jsontext);
for (i = 0; i < getContact.popularresult.length; i++) {
alert(getContact.popularresult[i].term);
}
http://jsfiddle.net/ae8gd/
If you get the jsonObject as shown then
var JsonArray=json.popular; //get jsonArry
$.each(JsonArray,function(i,val){
// do logic
});
To parse json use JSON.parse();