I have the following JSON:
{"title":"MYTITLE","employer":"MYEMPLOYER","weekends":[["6"],["8"],["15"]]}
I need to access weekends to get this:
6
8
15
Here is my try:
var json = $.parseJSON(data);
for (var i = 0, len = data.weekends.length; i < len; i++) {
console.log(data.weekends[i]);
}
But results are empty in chrome console log...if I understand correctly...i read length of json converted to an array and for in loop I read the value in the index array.
But I always get the error:
Uncaught TypeError: Cannot read property 'length' of undefined
Why is weekends length undefined? I set command length and it does not recognize it is an array and to count length so that for loop can work.
The problem is that you are accessing weekend using data, which is just raw JSON String. You need to access the json variable, which is the parsed JSON.
Below is the working code:
const data = {"title":"MYTITLE","employer":"MYEMPLOYER","weekends":[["6"],["8"],["15"]]};
// To avoid calculation of length on every iteration
const weekendLength = data.weekends.length;
for(let i = 0; i < weekendLength; i++){
console.log(data.weekends[i][0]);
}
Here, I am setting the JSON data directly to a variable. In your case, you are most probably getting the data from a network request. In that case, as you did in your code, you need to parse it first. (By calling $.parseJSON())
var data = {"title":"MYTITLE","employer":"MYEMPLOYER","weekends":[["6"],["8"],["15"]]};
for (var i = 0, len = data.weekends.length; i < len; i++){
console.log(data.weekends[i]);
}
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
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));
}
}
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();
}
}
}
}
I am calling a function this way .
var responseinner = returnvalues(selectedeleemnt);
console.log(responseinner);
displaying as Object
console.log(JSON.stringify(responseinner));
displaying as [{"name":"Coke","image":"json_images/coke_can.jpg","type":["250ml","300ml"],"price":["50","60"]}]
I tried all the ways of parsing this value
[{"name":"Coke","image":"json_images/coke_can.jpg","type":["250ml","300ml"],"price":["50","60"]}]
But always it throws the error as
Uncaught TypeError: Cannot read property 'length' of undefined
I used JSON.parse , JSON.stringfy() . but none helped .
for (var i = 0; i < responseinner.type.length; i++) {
}
could anybody please help me
for(var i=0;i<responseinner.length;i++){
responseinner[i].type .... }
Your responseinner isn't an Object but an Array with only one element.
So, you'll have to use responseinner[0].type.length
If it was an object, it would have started with {} and not [], with what arrays start.
You can use of
for (let element of responseinner) {
// do something../
console.log(element.name)
}
To parse json with jquery:
$.each($.parseJSON(responseinner), function(key,value){
//do something
});
If responseinner is already formed as a json you don't need the $.parseJSON
or
responseineer[0].name
responseineer[0].image
...
Json data comes into an array so get it's object with an index, so to access it: responseineer[0]
try test[0].type.length you have an Object in an array of length 1 therefore you must first define the index location of the Object in the array which is trivial since the array has 1 defined index.
for (var i = 0; i < responseinner[0].type.length; i++) {
}
for(var i=0;i
Its works fine but...how to assign to gridview in client side
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
}