Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a basic Node.js script that creates a JSON array from a few variables and other arrays.However, whenever I call on myJSON.title in my code it does not work at all. It gives undefined. Could anyone please help me?
for (var i = 0; i < route.length; i++) {
var item = {
"title": title[i],
"date": date[i],
"summary": summary[i],
"route": route[i],
"pebbleid": "geoquery-" + timegreeting + route[i]
};
myarray.push(item);
for (var i = 0; i < route.length; i++) {
var item = {
"title": title[i],
"date": date[i],
"summary": summary[i],
"route": route[i],
"pebbleid": "geoquery-" + timegreeting + route[i]
};
myarray.push(item);
}
myJSON = JSON.stringify({delays: myarray});
console.log(myJSON);
}
The reason myJSON.title is undefined is that JSON.stringify returns a string, and strings don't have a title property.
The object represented by the JSON string myJSON has a single property, delays, (because that's what you put in JSON.stringify). The value of that property is an array. Each element of that array is an object that has a title property (and date, summary, etc.). So to get any one title, you first have to retrieve the delays array and then retrieve one of its elements by index, and then retrieve that element's title property.
So for e.g. the 0th element in delays, you would do this:
obj.delays[0].title
I used "obj" because this will not work with myJSON, because like I said, in the code you've shown myJSON is a JSON string, not a JavaScript object, and so doesn't have a delays property.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have keyword (for example -2330) that needs to be compared with the 'values' in this string below.
"[{\"type\":\"A_PRODUCT\",\"value\":[[{\"key\":\"SUBCLASS\",\"value\":\"1574\"}],[{\"key\":\"SUBCLASS\",\"value\":\"2331\"}]]}]";
Expected output needs to be true or false depending if the string has the keyword or not.
How do I check it?
I will do something like this to loop
var a = "[{\"type\":\"A_PRODUCT\",\"value\":[[{\"key\":\"SUBCLASS\",\"value\":\"1574\"}],[{\"key\":\"SUBCLASS\",\"value\":\"2331\"}]]}]";
var new_a = JSON.parse(a);
var value_compare = '1574';
new_a[0]['value'].forEach(element => {
if (element[0].value == value_compare) {
//DO SOMETHING
alert('found: '+ JSON.stringify(element));
}
});
You first need to parse the JSON into an suitable JS structure. Because of the nested nature of the data you need to 1) map over the first object in your array, 2) return the value of the value property of the first object of each and finally 3) check to see if the keyword is included in the the returned array, and return true or false.
const json = '[{\"type\":\"A_PRODUCT\",\"value\":[[{\"key\":\"SUBCLASS\",\"value\":\"1574\"}],[{\"key\":\"SUBCLASS\",\"value\":\"2331\"}]]}]';
const data = JSON.parse(json);
function doesItExist(data, keyword) {
const values = data[0].value.map(arr => arr[0].value);
return values.includes(keyword);
}
console.log(doesItExist(data, '2331'));
console.log(doesItExist(data, 'Bob'));
console.log(doesItExist(data, '1574'));
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
https://jsfiddle.net/a/2L4t9saq/217/ is my fiddle
most of the code you can ignore, here is the function:
var modGrid = function(code){
var arr = code
console.log(arr)
for(var n=1;n<gridx+1;n++){
for(var i = 1; i<gridy+1; i++){
var garbledMess = "[x="+i+"][y="+n+"]"
var idea = arr[0]
arr.shift()
$(garbledMess).css("background-color",idea)
}
}
}
the syntax error is as follows:
Uncaught TypeError: arr.shift is not a function
at modGrid ((index):44)
at window.onload ((index):81)
since the modGrid function takes in an array (in the case of my code an array of 4 elements) the .shift() function should be removing the first option in the array, it worked before i added some more code, but now it is apparently not a function
many thanks
since the modGrid function takes in an array
It is designed to take an array, but that isn't what you are passing it.
You are passing it a string, another string, a number and another number.
modGrid('rgba(255,0,0,1)','rgba(0,255,0,1)',2,1);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Can anybody provide the java script for the below JSON. I tried in many way, but could not add the field "set"
{
"student":[
"set",
[
{
"name":"abcd",
"id":"1234"
}
]
]
}
So your javaScript variable would be an objecthaving property/key name student of array type. Now student has two elements set a string and an object, other element is also an array, has an element of object type. This element has two properties/keys name and id.
var required = {};
required.student = [];
required.student.push("set");
var innerArray = [];
var innerObj = {};
innerObj.name = "abcd";
innerObj.id = "1234";
innerArray.push(innerObj);
required.student.push(innerArray);
document.write('<pre> ' + JSON.stringify(required,0, 3) + '</pre>');
JSON.parse(jsonString);
Is a pure JavaScript approach so long as you can require a reasonably modern browser.
See also https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
I'm not sure what you are trying to do, but...
If you just want to have the JSON object you described available in your JavaScript code, you can just put it into a variable.
var json = {
"student":[
"set",
[
{
"name":"abcd",
"id":"1234"
}
]
]
};
// Testing the object:
// Print the JSON object we made, as a string
console.log(JSON.stringify(json));
// Print the first item inside the 'student' array
console.log(json.student[0]);
If you instead have your JSON as a string, you can parse it to JSON object with:
var json = JSON.parse(jsonString);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a json array like this
AlreadySaved:[
{
"SNO":1,
"SNUMBER":"827",
"STARTDATE":"11/12/2016",
"STARTTIME":"03:06:50 PM",
"ITEMNAME":"KBand",
"ITEMCODE":"KB2541",
"ITEMSKUNAME":"Band",
"ITEMSKUCODE":"BT102",
"SALESRATE":100.0,
"PURCHASERATE":5.0,
"DOE":"~",
"STOCKQTY":2.0,
"PHYSICALQTY":1.0
}
]
I need to fetch value of PHYSICALQTY and display it in alert.I am new to jquery/javascript so any help would be appreciated.
You can access json like normal objects in js via dot notation.
var json = {"STOCKQTY":2.0,"PHYSICALQTY":1.0};
console.log(json.PHYSICALQTY);
If you Have saved this json under another object You need to go deeper, eg.
var response = {AlreadySaved: [{"STOCKQTY":2.0,"PHYSICALQTY":1.0}] };
console.log(response.AlreadySaved[0].PHYSICALQTY);
Please remember also that in some cases You may have json (eg from response) as string not object.
console.log(typeof someJson); //string
In that cases You need to parse this string into json
var json = JSON.parse(someJsonAsString);
Happy hacking!
You can use json[0].PHYSICALQTY
var json = [{
"SNO": 1,
"SNUMBER": "827",
"STARTDATE": "11/12/2016",
"STARTTIME": "03:06:50 PM",
"ITEMNAME": "KBand",
"ITEMCODE": "KB2541",
"ITEMSKUNAME": "Band",
"ITEMSKUCODE": "BT102",
"SALESRATE": 100.0,
"PURCHASERATE": 5.0,
"DOE": "~",
"STOCKQTY": 2.0,
"PHYSICALQTY": 1.0
}];
document.getElementById('id1').innerHTML=json[0].PHYSICALQTY;
<div>
PHYSICALQTY:<label id="id1"></label>
</div>
You can parse this and then get the value. Ouf course if you receive this like a string.
var a = '[{"SNO":1,"SNUMBER":"827","STARTDATE":"11/12/2016","STARTTIME":"03:06:50 PM","ITEMNAME":"KBand","ITEMCODE":"KB2541","ITEMSKUNAME":"Band","ITEMSKUCODE":"BT102","SALESRATE":100.0,"PURCHASERATE":5.0,"DOE":"~","STOCKQTY":2.0,"PHYSICALQTY":1.0}]'
var output = JSON.parse(a)
console.log(output[0].PHYSICALQTY)
Your JSON object is not valid, you can check that at http://jsonlint.com/
Once you set it to something like
var AlreadySaved =[{"SNO":1,"SNUMBER":"827","STARTDATE":"11/12/2016","STARTTIME":"03:06:50 PM","ITEMNAME":"KBand","ITEMCODE":"KB2541","ITEMSKUNAME":"Band","ITEMSKUCODE":"BT102","SALESRATE":100.0,"PURCHASERATE":5.0,"DOE":"~","STOCKQTY":2.0,"PHYSICALQTY":1.0}] ;
you can access it via AlreadySaved[0]["PHYSICALQTY"]
It is not json object.
Json:
{AlreadySaved:[{"SNO":1,"SNUMBER":"827","STARTDATE":"11/12/2016","STARTTIME":"03:06:50 PM","ITEMNAME":"KBand","ITEMCODE":"KB2541","ITEMSKUNAME":"Band","ITEMSKUCODE":"BT102","SALESRATE":100.0,"PURCHASERATE":5.0,"DOE":"~","STOCKQTY":2.0,"PHYSICALQTY":1.0}]}
In this case you may get your property:
alert(AlreadySaved[0].PHYSICALQTY);
firstly you have to serialize your object like this;
var json_data = JSON.parse(AlreadySaved);
and if you know specific data in json_data, you can call with index number like this;
var result= json_data[0]["PHYSICALQTY"];
Or you can find whatever you want, you can find with foreach;
for(var item in json_data) {
alert(item["PHYSICALQTY"].value);
}
Please try this:
//In case you are getting this from ajax request put "returnObject.Json_Variable" instead of "Json_Variable"
var obj = (JSON.parse("Json_Variable"));
var resultPHYSICALQTY=obj.PHYSICALQTY);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I seem to be having an issue with my JSON objects, which is unusual because I have one block of code I know works:
function gotoPassage(id) {
for (var i = 0; i < passages.length; i++) {
if (passages[i].id == id) {
if (passages[i].bgImage)
changeBGImage(passages[i].bgImage);
$("#passage").hide();
$("#passage").html(passages[i].content);
$("#passage").fadeIn(1000);
}
}
}
The above code works as expected, so I assumed the same logic and applied it to my other code, which - by the way - does not render correctly. This code currently looks something like this:
function showLoreHistory() {
$("#lore ul").html("");
for (var i = 0; i < foundLore.length; i++) {
var content = "<li><a href='javascript:gotoLore(" + foundLore[i].id ");'>";
for (var j = 0; j < lore.length; j++) {
if (lore[j].id == foundLore[i].id) {
content += lore[j].title + "</a></li>";
$("#lore ul").append(content);
break;
}
}
}
$("#lore").toggle();
}
The structure of lore and foundLore look like this:
var lore = [{
"id": 0,
"title": "The First War",
"image": "Images/The First War.jpg",
"pages": [{
"content": ""
}]
}];
var foundLore = [{
"id": 0
}];
I was thinking of iterating through the keys of each JSON object, but found that it looked ugly, and considering my previous logic for showing passages worked assumed that the same logic would work for my Lore, but it does not seem to.
I would come up with a JSFiddle but unfortunately I rely on too many local resources to get it working properly without sacrificing code, which would defeat the point of me explaining the issue.
EDIT: If I comment out the for loops in the function showLoreHistory, the page will render correctly as expected in the following image.
EDIT 2: What I am trying to do is get the values from the above JSON object and adding the values to HTML, under a div tag namely lore - which is hidden by default.
The Div tag, with its content, looks like this:
<div id="lore">
<h1>Lore</h1>
<ul>
</ul>
</div>
If you have any solutions regarding this, I would strongly appreciate it. Thank you.
this line
var content = "<li><a href='javascript:gotoLore(" + foundLore[i].id ");'>";
is missing a '+' after foundLore[i].id
Also, the toggle is probably not what you want. I think you should hide it at the beginning of the function, then show it at the end like in your other function where you show the image.
If you open developer tools (f12 or ctrl+shift+j) it will show you errors like that.
p.s. it took literally under 2 minutes to make a fiddle for me to find the problem.. Give it a shot next time.
http://jsfiddle.net/8cr9t1zy/2/
You have to hit the button twice because of the toggle() thing I mentioned above.