Arrays#
An array is an ordered list of values, indexed from 0.
Indore 3 Ujjain [ 'Jabalpur', 'Indore', 'Bhopal', 'Ujjain', 'Dewas' ] Dewas [ 'Jabalpur', 'Indore', 'Bhopal', 'Ujjain' ] true 3 [ 'Indore', 'Bhopal' ] Jabalpur | Indore | Bhopal | Ujjain
| Method | Does | Changes the array? |
|---|---|---|
push, pop |
add / remove at the end | yes |
unshift, shift |
add / remove at the start | yes |
splice(i, n, ...items) |
remove n items at i (and insert) |
yes |
slice(a, b) |
copy from a up to (not including) b |
no |
includes, indexOf |
search | no |
join(sep) |
make a string | no |
sort, reverse |
reorder | yes |
map, filter, reduce, find, forEach#
These higher-order methods take a callback and are used in almost every real JavaScript program.
- map - हर item को बदलकर नई list बनाता है (जैसे हर दाम में GST जोड़ना)।
- filter - सिर्फ़ वो items रखता है जो शर्त पूरी करें (जैसे 200 से सस्ते)।
- reduce - पूरी list को मिलाकर एक value बनाता है (जैसे कुल जोड़)।
Sorting numbers correctly#
[ 10, 100, 25, 9 ] [ 9, 10, 25, 100 ] [ 100, 25, 10, 9 ]
Objects#
An object groups related values as key: value pairs - like one row of a register.
Asha 20 Indore I am Asha from Indore [ 'name', 'age', 'address', 'intro', 'email' ] asha@mail.com
Arrays of objects#
Real data - API results, database rows, spreadsheets - is usually an array of objects.
[ 'Ravi', 'Asha' ] 4300
Destructuring and spread#
Imran Ujjain
5 8 [ 13, 21, 34 ]
[ 1, 2, 3, 4, 5 ]
{ name: 'Imran', city: 'Indore', role: 'student' } UjjainJSON - data for the web#
JSON (JavaScript Object Notation) is the text format APIs use to send data. Keys and strings use double quotes.
{"title":"JavaScript","lessons":7,"free":true} string
SQL 9Summary#
- Arrays: indexed lists with
push/pop,slice/splice,includes,join,sort. maptransforms,filterselects,reducecombines,findfinds the first match.- Sort numbers with
sort((a, b) => a - b). - Objects store
key: valuepairs; arrays of objects represent real data. - Destructuring and spread (
...) make copying and unpacking easy; JSON moves data over the web.