Click here to Skip to main content
15,886,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Ppl,

I am trying to loop the Json ouptut into for loop and store them into another array as i need only specific values from Json....

here is below code... i am unable to fetch DTSTART,DTEND fields.. i guess its because of naming.. i wont be able to change the param names as i receive it from output.

Any idea how to get the value of those ids also? Thanks

<!DOCTYPE html>
<html>
<body>

<p>Looping through arrays inside arrays.</p>

<p id="demo"></p>

<script>

var myObj, i, j, x = "";
myObj = {
      "cars": [
        {
		"DTSTART;TZID=America/New_York": "20170211T123000",
		"DTEND;TZID=America/New_York": "20170211T133000",
		"RRULE": "FREQ=WEEKLY;BYDAY=SA",
		"DTSTAMP": "20170213T195454Z",
		"UID": "ks0lgptphtr46rdt4a6p94l2f8@google.com",
		"CREATED": "20170211T152911Z",
		"DESCRIPTION": "",
		"LASTMODIFIED": "20170211T152911Z",
		"LOCATION": "",
		"SEQUENCE": "0",
		"STATUS": "CONFIRMED",
		"SUMMARY": "monday meeting",
		"TRANSP": "OPAQUE"
	}
    ]
}

for (i in myObj.cars) {
    x += "<h1>" + myObj.cars[i].SUMMARY + "</h1>";
     x += "<h1>" + myObj.cars[i].DTSTART + "</h1>";
   
}

document.getElementById("demo").innerHTML = x;

</script>

</body>
</html>


What I have tried:

Here is code for execution

Tryit Editor v3.3[^]
Posted
Updated 17-Feb-17 17:43pm
Comments
Patrice T 17-Feb-17 21:15pm    
put your code in question, not a link
Member 12974741 17-Feb-17 21:35pm    
I did... the question part has same code... you were not able to see?
Karthik_Mahalingam 17-Feb-17 23:40pm    
Always use  Reply   button to post comments/query to the concerned user, so that the user gets notified and respond to your text.
Member 12974741 17-Feb-17 23:53pm    
ah...got it...I think i missed previous time.. Thanks for that..Hope you are doing great!
Karthik_Mahalingam 17-Feb-17 23:54pm    
welcome :)

Ok, the undefined error because of the key naming, the key name is "DTSTART;TZID=America/New_York" and not "DTSTART". Based on the circumstances you describe, here is an alternative to access the array value using key like/startsWith. Now, your next assignment is to figure out how to display the result in the order that you prefer because the loop start from top to bottom.

JavaScript
for (var i = 0; i < myObj.cars.length; i++){
    var obj = myObj.cars[i];
    for (var key in obj){
    
    	if (key == "SUMMARY") {
         	x += "<h1>" + obj[key] + "</h1>";
        }
        
        if (key.startsWith("DTSTART")) {
         	x += "<h2>" + obj[key] + "</h2>";
        }
    }
}


Output:
20170211T123000
monday meeting
 
Share this answer
 
Comments
Member 12974741 17-Feb-17 23:39pm    
It helps...Thanks alot!!
Bryian Tan 18-Feb-17 0:21am    
You're welcome
Karthik_Mahalingam 17-Feb-17 23:40pm    
5
Bryian Tan 18-Feb-17 0:20am    
Thanks.
Karthik_Mahalingam 18-Feb-17 0:21am    
welcome :)
It is as simple as adding a square [], i.e.
myObj.cars[i]["DTEND;TZID=America/New_York"]
Nevertheless, try to avoid unusual keys like this as it is going to break those usual JSON handling code.
 
Share this answer
 
v3
Comments
Member 12974741 17-Feb-17 23:55pm    
This seems to be cool solution.... it makes things easier.. Appreciated Peter!!
Peter Leow 17-Feb-17 23:57pm    
Glad that it helps.
Bryian Tan 18-Feb-17 0:08am    
Nice!!!, Not sure what I was thinking, I tried myObj.cars[i].["DTEND;TZID=America/New_York"] earlier and that didn't work. That why I come out with the loop idea.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900