I am trying to add find a property of an object using a variable but it is not working. When I write the object name iso of the variable, it is working, although the variable and the object name are exactly the same..
function calCulate(nrs){
const product = {DE2599:{name:"tet", bar:2.5, price: 3.5}};
var nr = document.getElementById(nrs).value;
var sku = ("prod" + nr);
var qty = ("qty" + nr);
var pric = ("price"+nr);
var pal = ("pallet"+nr);
var ctry = document.getElementById('country').value;
var x = document.getElementById(qty).value;
var test = document.getElementById(prod).value;
var tests = test.toString(); // way of getting the result
var testr = tests.indexOf(" "); // way of getting the result
var testt = tests.slice(0,testr); // way of getting the result
var uniqueID = ctry+testt; // this value returns DE2599
var mywindow = window.open('','PRINT','height=800,width=800');
var ter = product.uniqueID; // I use uniqueID because it can vary
var tar = ter.price;
mywindow.document.write(tar); // this is not showing any result; but if I change the uniqueID with the actualy key, ie DE2599, it is working correctly
uniqueId is not a property of product so your ter will be storing undefined (and you should be getting an error for tar).
When you need to use the value of a variable to access the property of an object, you need to use bracket notation.
const product = {DE2599:{name:"tet", bar:2.5, price: 3.5}};
let uniqueId = 'DE2599';
console.log('with dot notation');
console.log(product.uniqueId);
console.log('with bracket notation');
console.log(product[uniqueId]);
i have a multidimensional json . i want to parse it to get the values.
var json='{"Links":[],"RequestedObject":{"FieldContents":{"21514":{"Type":1,"IsError":false,"Value":"Saneen","FieldId":21514,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21516":{"Type":1,"IsError":false,"Value":"English","FieldId":21516,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21517":{"Type":1,"IsError":false,"Value":"Malayalam","FieldId":21517,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21515":{"Type":2,"IsError":false,"Value":26.0,"FieldId":21515,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21518":{"Type":2,"IsError":false,"Value":80.0,"FieldId":21518,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21519":{"Type":2,"IsError":false,"Value":40.0,"FieldId":21519,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21520":{"Type":4,"IsError":false,"Value":{"ValuesListIds":[72639],"OtherText":null},"FieldId":21520,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21523":{"Type":3,"IsError":false,"Value":"2017-03-29T00:00:00","FieldId":21523,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21510":{"Type":6,"FieldId":21510,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21511":{"Type":21,"FieldId":21511,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21512":{"Type":22,"FieldId":21512,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21521":{"Type":11,"Value":null,"FieldId":21521,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}}}},"IsSuccessful":true,"ValidationMessages":[]}';
i have tried with JSON.parse , but no luck.
Here is an simple example of JSON.parse() and here is document for that:
var json='{"Links":[],"RequestedObject":{"FieldContents":{"21514":{"Type":1,"IsError":false,"Value":"Saneen","FieldId":21514,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21516":{"Type":1,"IsError":false,"Value":"English","FieldId":21516,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21517":{"Type":1,"IsError":false,"Value":"Malayalam","FieldId":21517,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21515":{"Type":2,"IsError":false,"Value":26.0,"FieldId":21515,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21518":{"Type":2,"IsError":false,"Value":80.0,"FieldId":21518,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21519":{"Type":2,"IsError":false,"Value":40.0,"FieldId":21519,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21520":{"Type":4,"IsError":false,"Value":{"ValuesListIds":[72639],"OtherText":null},"FieldId":21520,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21523":{"Type":3,"IsError":false,"Value":"2017-03-29T00:00:00","FieldId":21523,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21510":{"Type":6,"FieldId":21510,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21511":{"Type":21,"FieldId":21511,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21512":{"Type":22,"FieldId":21512,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}},"21521":{"Type":11,"Value":null,"FieldId":21521,"UpdateInformation":{"CreateDate":null,"UpdateDate":null,"CreateLogin":null,"UpdateLogin":null}}}},"IsSuccessful":true,"ValidationMessages":[]}';
var obj = JSON.parse(json);
console.log(obj);
// retrieve particular fields
var newFieldContents = {};
for(var key in obj['RequestedObject']['FieldContents']){
var o = obj['RequestedObject']['FieldContents'][key];
newFieldContents[key] = {
Value: o['Value'],
FieldId: o['FieldId']
}
}
console.log(newFieldContents);
Update
Add how to retrieve particular fields.
i got a words array in JSON form like:
var words = [];
words.push({word:"I",x:50,y:50});
and i want it to store into a txt file which user can download from the website.
...
var data = JSON.stringify(words);
var file = new Blob([data],{type:'plain/text'});
...
however the output file is like this:
{"word":"Peaceful","x":40,"y":65,"lyric":"lyric"}
but i want it only print out the word "peaceful", i tried to use
var data = JSON.stringify(words.word);
but the output shows undefined.
words is an array, so you have to index it to access the object that you pushed onto it:
var data = JSON.stringify(words[0].word);
If I understand you correctly.
var myWords = [];
(for var i = 0; i < words.length; i++) {
var word = words[i].word;
myWords.push(word);
}
myWords.join(' '); // or myWords.join('\n');
I need the basic Help , I have a json object in the following format
Data = {"line":"1",
"column":"1"
"response":[{"criteria":"starts","response":"358","field":"S"},
{"criteria":"ends","response":"359","field":"H"}]}
I can get the Line and Column value as below
var obj = $.parseJSON(data);
var line = obj['line'];
var column=obj['column'];
I have tried below format but i cannt get response value:
var res = new Array(obj['response']);
alert(res[0]['criteria']);
And
var jsonObject = $.parseJSON(obj['response']);
var innerArray = jsonObject['response'];
alert(innerArray[0].fieldvalue);
how can i get the values in obj['response'] ? Any one could help me to find the solution
Why do you need to create a new array?
var res = new Array(obj['response']);
alert(res[0]['criteria']);
It is already converted to an array after you parse the JSON string, so this will work:
var res = obj['response'];
alert(res[0]['criteria']);