Unable to access json values - javascript

I have a JSON array from ajax response but when I tried to access it field then it says undefined.
here is my JSON
[{"material_name":1042,"qty_per_piece":"30","material_qty_req":150},{"material_name":1043,"qty_per_piece":"20","material_qty_req":100},{"material_name":1041,"qty_per_piece":"10","material_qty_req":50}]
what i am trying its following
for(var j = 0; j<material.length; j++){
var matName = material.material_name[j];
alert(matName);
when i tried through following it shows undefined
for(var j = 0; j<material.length; j++){
var material_names = material[j].material_name;
alert(material_names);

var material = [{"material_name":1042,"qty_per_piece":"30","material_qty_req":150},{"material_name":1043,"qty_per_piece":"20","material_qty_req":100},{"material_name":1041,"qty_per_piece":"10","material_qty_req":50}];
material.forEach(function(v,k){
console.log(v.material_name);
});

Related

JS JSON process

I'm new in JSON handling, so I would like to get some help :)
I have this data in the data variable:
{"json":null,"id":"1111","prime1":"26","prime2":"0","ass1":"0","ass2":"0","time1":"07:00:00","time2":"14:30:00"}*{"json":null,"id":"2111","prime1":"0","prime2":"0","ass1":"0","ass2":"0","time1":"07:00:00","time2":"14:30:00"}*{"json":null,"id":"3111","prime1":"11","prime2":"0","ass1":"0","ass2":"0","time1":"07:00:00","time2":"14:30:00"}*{"json":null,"id":"4111","prime1":"4","prime2":"0","ass1":"17","ass2":"13","time1":"07:00:00","time2":"17:30:00"}*{"json":null,"id":"5111","prime1":"6","prime2":"0","ass1":"23","ass2":"0","time1":"07:00:00","time2":"14:30:00"}*{"json":null,"id":"6111","prime1":"1","prime2":"0","ass1":"15","ass2":"0","time1":"07:00:00","time2":"14:30:00"}*{"json":null,"id":"1112","prime1":"0","prime2":"0","ass1":"0","ass2":"0","time1":"14:00:00","time2":"21:30:00"}*{"json":null,"id":"2112","prime1":"0","prime2":"0","ass1":"0","ass2":"0","time1":"14:00:00","time2":"21:30:00"}*{"json":null,"id":"3112","prime1":"22","prime2":"0","ass1":"18","ass2":"0","time1":"14:00:00","time2":"21:30:00"}*{"json":null,"id":"4112","prime1":"0","prime2":"0","ass1":"0","ass2":"0","time1":"14:00:00","time2":"21:30:00"}*{"json":null,"id":"5112","prime1":"3","prime2":"0","ass1":"19","ass2":"0","time1":"14:00:00","time2":"21:30:00"}*{"json":null,"id":"6112","prime1":"9","prime2":"0","ass1":"0","ass2":"0","time1":"14:00:00","time2":"21:30:00"}*{"json":null,"id":"1121","prime1":"3","prime2":"0","ass1":"15","ass2":"0","time1":"07:00:00","time2":"14:30:00"}*{"json":null,"id":"2121","prime1":"6","prime2":"0","ass1":"23","ass2":"0","time1":"07:00:00","time2":"14:30:00"}*{"json":null,"id":"3121","prime1":"11","prime2":"0","ass1":"0","ass2":"0","time1":"07:00:00","time2":"14:30:00"}*{"json":null,"id":"4121","prime1":"8","prime2":"0","ass1":"17","ass2":"13","time1":"07:00:00","time2":"14:30:00"}*{"json":null,"id":"5121","prime1":"22","prime2":"0","ass1":"19","ass2":"0","time1":"07:00:00","time2":"14:30:00"}*{"json":null,"id":"6121","prime1":"1","prime2":"0","ass1":"12","ass2":"0","time1":"07:00:00","time2":"14:30:00"}*{"json":null,"id":"1122","prime1":"0","prime2":"0","ass1":"0","ass2":"0","time1":"14:00:00","time2":"21:30:00"}*{"json":null,"id":"2122","prime1":"0","prime2":"0","ass1":"0","ass2":"0","time1":"14:00:00","time2":"21:30:00"}*{"json":null,"id":"3122","prime1":"0","prime2":"0","ass1":"0","ass2":"0","time1":"14:00:00","time2":"21:30:00"}*{"json":null,"id":"4122","prime1":"8","prime2":"0","ass1":"13","ass2":"18","time1":"14:00:00","time2":"21:30:00"}*{"json":null,"id":"5122","prime1":"22","prime2":"0","ass1":"19","ass2":"0","time1":"14:00:00","time2":"21:30:00"}
I can't create objects from them.
This is my code:
var dataSplit = data.split("*");
for (var i = 0; i < dataSplit.length; i++) {
objects[i] = dataSplit[i];
document.getElementById("proba").innerHTML += objects[i].time2;
}
Thanks for all of you!
It's working now, it was 2 hours for me. The problem was I didn't initialize the object array and didn't use JSON.parse() (tried the JSON.parse() before, without success). So the solution is:
var objects = [];
var dataSplit = data.split("*");
for (var i = 0; i < dataSplit.length; i++) {
objects[i] = JSON.parse(dataSplit[i]);
document.getElementById("proba").innerHTML += objects[i].time2;
}

String (split and loop ) to Array

can someone tell me where the below code is missing to get the correct data..
var temprule="MO,WE";
var rulest =[];
var jsondata=[];
rulest=temprule.split(',');
console.log("TTTTT rulest", rulest);
for(var j = 0; j < rulest.length; j++)
{
var day=rulest[j];
entry.day=day;
console.log(entry.day) // log shows MO and WE as per loop
jsondata.push(entry); // but jsondata has 2 entries with day WE
}
incorrect o/p ->[{day=WE},day=WE}] expected--> [{day=MO},day=WE}]
The variable entry needs to be initialized inside the loop.
var temprule="MO,WE";
var rulest =[];
var jsondata=[];
rulest=temprule.split(',');
console.log("TTTTT rulest", rulest);
for(var j = 0; j < rulest.length; j++)
{
var entry={};
var day=rulest[j];
entry.day=day;
console.log(entry.day) // log shows MO and WE as per loop
jsondata.push(entry); // but jsondata has 2 entries with day WE
}
console.log(JSON.stringify(jsondata));
Here is the jsfiddle link with corrected code.
JSFiddle Link

create an array from different objects

I've sent data from server side with socket.io :
for (i = 0; i<rows.length; i++) {
socket.emit('Switch', {eqid:rows[i].EquipmentID,eqroom:rows[i].Name});
}
and in the client side :
socket.on('Switch', function (data) {
console.log(data.eqid);
}
and what I get is :console log and when I do console.log(data.eqid[0] I get undefined
so I want to get an array [120336,120337..]
I've tried also to send an array from the beginning in the server side :
for (i = 0; i<rows.length; i++) {
var test=[];
test.push(rows[i].EquipmentID);
}
console.log(test);
console.log gives me the last equipmentID only [120339
console.log gives me the last equipmentID only [120339
Because you are redefining rows array in every iteration.
Try this:
var ids = [];
var names = [];
for (var i = 0; i < rows.length; i++) {
ids.push(rows[i].EquipmentID);
names.push(rows[i].Name);
}
socket.emit('Switch', {eqid: ids, eqroom: names});

How to get value of key from the array in java script

I have created static array as following below
country["0"]=[USA];
sate[country[0][0]]=[["NewYork","NY"],["Ohio,"Oh"]]
for (var i = 0; i < sate[country[0][0]].length; i++) {
var key = state[country[0][0]] [i][0];
var value = state[country[0][0]] [i][i+1];
}
from above loop i am able to get the keys of state like NewYork and Ohio.
Please help me how i will get the value of "NY" and "Oh"?
var value = state[country[0][0]] [i][1];
There are a couple or three errors in your code. Assuming country has a list of countries and state keeps the states of the country...
country = ["USA"];
state = {"USA": [["NewYork","NY"],["Ohio","Oh"]] };
for (var i = 0; i < state[country[0]].length; i++) {
var key = state[country[0]] [i][0];
var value = state[country[0]] [i][1];
}
You have mistyped here
sate[country[0][0]]=[["NewYork","NY"],["Ohio", "Oh"]]
and You can get ["NY", "Oh"] by using this:
for (var i = 0; i < sate[country[0][0]].length; i++) {
var key = state[country[0][0]] [i][0];
var value = state[country[0][0]] [i][1];
}

creating a dynamic binding in JavaScript

I'm implementing a feature which will allow me to dynamically add columns into a JavaScript table:
for(var i = 0; i < info.length; i++){
var temp = [];
temp.push(parseInt(info[i].site_id));
temp.push(info[i].site);
temp.push(info[i].site_code);
temp.push(processArray(info[i].tc10));
temp.push(processArray(info[i].tc9x_test));
temp.push(processArray(info[i].tc9x_build));
temp.push(processArray(info[i].oracle_dev));
temp.push(processArray(info[i].database));
temp.push(processArray(info[i].baseline));
temp.push(processArray(info[i].push_windows));
temp.push(processArray(info[i].push_unix));
temp.push(processArray(info[i].license));
temp.push(processArray(info[i].tcx));
temp.push(processArray(info[i].eng));
temp.push(processArray(info[i].perforce_proxy));
temp.push(processArray(info[i].volume_server));
temp.push(info[i].windows_ref_unit_location);
temp.push(info[i].unix_ref_unit_location);
temp.push(info[i].windows_rte_location);
temp.push(info[i].unix_rte_location);
temp.push(info[i].windows_toolbox_location);
temp.push(info[i].unix_toolbox_location);
temp.push(info[i].UGII_LICENSE_FILE);
temp.push(info[i].UGS_LICENSE_SERVER);
temp.push(info[i].unix_dev_units);
temp.push(info[i].unix_devop_path);
temp.push(info[i].perforce_proxy_path);
temp.push(info[i].primary_contact);
temp.push(info[i].secondary_contact);
temp.push(info[i].num_users);
temp.push(info[i].TC_12);
// check if new columns got added:
if(len > 29){
for(var j = 30; j < len; j++){
var col = columns[j];
temp.push(info[i].col);
}
}
rows.push(temp);
}
return rows;
}
var rows = [[]] holds the table data ... info[[]] contains the JSON objects queried from the database.
The problem in on this piece of code:
for(var j = 30; j < len; j++){
var col = columns[j];
temp.push(info[i].col);
}
I'm trying to dynamically bind col with some of the attributes of info. But I don't know whether is possible or not ... How could I do that?
Suppose a user added a new column, TC_12. Thus, I don't know TC_12 exists, so I want to dynamically bid col into info[i] so it could somehow yield me info[i].TC_12.
Any ideas?
Use square bracket notation to use the value of a variable or the result of some other expression as the object property.
temp.push(info[i][col]);
FYI, you can do all those pushes with a single call to .push() by passing multiple arguments...
temp.push(parseInt(info[i].site_id),
info[i].site,
info[i].site_code,
processArray(info[i].tc10),
processArray(info[i].tc9x_test),
// etc...
);

Categories