Theory
એક event ક્યારેય point નહોતું
Last lesson એ single event record ને perfected કર્યું. પણ FestConnect ની reality plural છે: 40 events ની LIST, ticket prices ની list, hall નું seat map booked-or-free ની rows અને columns સાથે.
XML એ lists ને elements ને repeat કરીને faked કર્યા. JSON પાસે real thing છે: array, ordered values square brackets માં, અને તમારું syllabus તેના 5 shapes ને names કરે છે. બધા 5 એક grammar ને obey કરે છે; શું changes થાય છે તે ફક્ત શું brackets ની અંદર sits કરે છે.
Theory
array, ઔપચારિક રીતે
JSON array એ ordered, comma-separated values ની list છે square brackets માં:
["Garba Night", "Coding Contest", "Robo Race"]
Properties જે matter કરે છે:
- ordered: position એ meaning છે; parsing પછી, 0 થી index કરો
- values કોઈ પણ JSON type ના હોઈ શકે: strings, numbers, booleans, null, objects, arrays
.lengthelements ને count કરે છે parsing પછી- એક array માં types ને mix કરવું legal છે, though real data દરેક array ને consistent રાખે છે
At a glance
5 syllabus forms, એક fest દરેક
| Form | Example | FestConnect meaning |
|---|---|---|
| Array of strings | ["Garba Night", "Robo Race"] | Event names |
| Array of numbers | [50, 100, 150] | Ticket price tiers |
| Array of booleans | [true, true, false] | કયા fest days open છે |
| Array of objects | [{"name": "Garba Night", "seats": 350}, ...] | THE events list itself |
| Multi-dimensional | [[1, 1, 0], [0, 1, 1]] | Seat map: seats ની rows |
Practical
બધા 5 forms, parsed અને walked (run it)
const text = `{
"names": ["Garba Night", "Coding Contest", "Robo Race"],
"prices": [50, 100, 150],
"daysOpen": [true, true, false],
"events": [
{ "name": "Garba Night", "seats": 350 },
{ "name": "Coding Contest", "seats": 60 }
],
"seatMap": [[1, 1, 0],
[0, 1, 1]]
}`;
const d = JSON.parse(text);
console.log(d.names[0]); // Garba Night (index from 0)
console.log(d.prices.length); // 3
console.log(d.events[1].name); // Coding Contest: index, પછી key
console.log(d.seatMap[1][2]); // 1: row index 1, seat index 2
for (const ev of d.events) { // the loop દરેક fest page runs કરે છે
console.log(ev.name + ": " + ev.seats + " seats");
}
This example runs in Gri-Learn on the web, where you can edit it and see the output.
Theory
chained access ને Reading કરવું
2 compound forms ને slow motion deserve કરે છે.
Array of objects: d.events[1].name right to left ને path તરીકે reads કરે છે: d માં, events લો (array), element index 1 લો (SECOND object), તેનું name લો. Index first, પછી key.
Multi-dimensional: d.seatMap[1][2] એ outer index પછી inner: row index 1 પર (બીજી row [0, 1, 1]), પછી તેનું element index 2 પર: value 1, booked seat. Row, પછી column, બંને 0 થી: same discipline જેમ BCA102 ના matrix positions.
Quiz
listing ના data ને use કરીને: શું d.events[0].seats evaluate થાય છે?
- 350: first event object નું seats value
- 60: arrays 1 થી index થાય છે, તેથી events[0] exist નથી કરતું
- key નું name "seats" itself
- undefined: arrays ની અંદર objects ને special accessor ની જરૂર છે
Show the answer
350: first event object નું seats value
Index 0 એ FIRST element છે (Garba Night નું object), અને .seats તેનું number pluck કરે છે: 350. Option B 1-based counting ને import કરે છે; JSON arrays, દરેક JavaScript array જેમ, 0 થી start થાય છે, તેથી events[1] એ Coding Contest છે. Option C key ના name ને તેના value સાથે confuse કરે છે. Option D ceremony ને invent કરે છે જે exist નથી કરતી: array element જે object છે તે ordinary dot access ને answer કરે છે, જે precisely છે શા માટે array-of-objects form real APIs ને dominate કરે છે: list માં index કરો, પછી તેને કોઈ પણ object જેમ use કરો.
Think first
Book seat row 0, seat 2
student listing ના seatMap ની FIRST row ની THIRD seat ને book કરે છે. tap કરતા પહેલાં: કયું expression તે seat નું current value ને reads કરે છે, શું છે તે હવે, અને booking તેને શું માં change કરે છે?
Show the answer
First row = index 0, third seat = index 2: d.seatMap[0][2], હાલમાં 0 (free, કારણ કે row 0 એ [1, 1, 0] છે). Booking d.seatMap[0][2] = 1 set કરે છે, row 0 ને fully booked બનાવે છે: [1, 1, 1]. habit internalise કરવા માટે: human counting (first, third) ને indexes (0, 2) માં translate કરો brackets ને touch કરતા પહેલાં, outer dimension first. Off-by-one અહીં wrong seat ને book કરે છે real person માટે, જે રીતે આ exam point હોવાનું બંધ થાય છે અને refund queue શરૂ થાય છે.
Watch out
Array slips
Human counting: events[1] એ SECOND event છે: eternal off-by-one.
Trailing comma: [50, 100, 150,] JSON.parse ને fail કરે છે, same law જેમ objects.
Assuming order survives everything: JSON arrays ordered છે, પણ object KEYS ને કોઈ order promise નથી: array positions પર rely કરો, ક્યારેય key positions પર નહીં.
2-row map પર seatMap[2][0]: row index 2 exist નથી કરતું; તેને reading undefined આપે છે અને તેમાં indexing throws કરે છે: જ્યારે rows vary થાય ત્યારે .length ને check કરો.
Theory
હવે તમે કોઈ પણ API response ને read કરો છો
Arrays of objects nested arrays સાથે એ real-world API data નું shape છે: weather feed, cricket scorecard, તમારા own future endpoints. Practice: tomorrow નું canteen menu ને JSON તરીકે sketch કરો (days array, દરેક day object, દરેક meal string array) અને chained access સાથે તેને walk કરો. એક JSON topic બાકી છે, અને તે curiosity છે: comment syntax જે JSON deliberately પાસે નથી, અને teams શું instead કરે છે.
Summary
Key takeaways
- Arrays: ordered, comma-separated values [ ] માં; 0 થી indexed parsing પછી; .length count કરે છે.
- પાંચ forms: strings, numbers, booleans, objects, અને arrays-in-arrays (multi-dimensional).
- Array of objects એ real-world king છે: d.events[1].name index, પછી key ને chain કરે છે.
- Multi-dimensional access outer-then-inner છે: seatMap[row][seat], બંને 0 થી.
- Element types legally mix થઈ શકે છે; consistent arrays sane arrays છે.
- Array order guaranteed છે; object key order નથી.
- Memory hook: brackets queue ને hold કરે છે, queue ને 0 થી count કરો.