⚡0 XP🔥0

Arrays and Objects

Lesson 5 of 7 ⏱ 5 min read ✎ 3 exercises ❓ 5-question quiz ▶ Runs JavaScript

Store lists and records - array methods (push, pop, slice, splice, includes, sort), map, filter, reduce and find, objects, arrays of objects, destructuring, spread and JSON.

Arrays#

An array is an ordered list of values, indexed from 0.

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
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.

▶ Try it YourselfJavaScript
Ctrl + Enter to run
हिंदी में समझें
  • map - हर item को बदलकर नई list बनाता है (जैसे हर दाम में GST जोड़ना)।
  • filter - सिर्फ़ वो items रखता है जो शर्त पूरी करें (जैसे 200 से सस्ते)।
  • reduce - पूरी list को मिलाकर एक value बनाता है (जैसे कुल जोड़)।

Sorting numbers correctly#

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
[ 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.

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
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.

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
[ 'Ravi', 'Asha' ] 4300

Destructuring and spread#

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
Imran Ujjain
5 8 [ 13, 21, 34 ]
[ 1, 2, 3, 4, 5 ]
{ name: 'Imran', city: 'Indore', role: 'student' } Ujjain

JSON - data for the web#

JSON (JavaScript Object Notation) is the text format APIs use to send data. Keys and strings use double quotes.

▶ Try it YourselfJavaScript
Ctrl + Enter to run
Output
{"title":"JavaScript","lessons":7,"free":true} string
SQL 9

Summary#

  • Arrays: indexed lists with push/pop, slice/splice, includes, join, sort.
  • map transforms, filter selects, reduce combines, find finds the first match.
  • Sort numbers with sort((a, b) => a - b).
  • Objects store key: value pairs; arrays of objects represent real data.
  • Destructuring and spread (...) make copying and unpacking easy; JSON moves data over the web.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Class report

Using map, filter and reduce on the marks array, print three lines:

With grace: [ 47, 92, 38, 75, 88 ]
Passed: [ 47, 92, 75, 88 ]
Average: 68

With grace adds 2 marks to every student. Passed keeps the grace marks that are 40 or more. Average is the average of the grace marks.

Expected output:
With grace: [ 47, 92, 38, 75, 88 ]
Passed: [ 47, 92, 75, 88 ]
Average: 68
2

Shopping bill

cart is an array of objects. Print each line as Soap x 3 = 120, then the total as Total: 590.

Expected output:
Soap x 3 = 120
Rice 5kg x 1 = 350
Biscuits x 4 = 120
Total: 590
3

Top scorer

Sort students by score from highest to lowest (without changing the original - use [...students]), then print the names in that order joined by , and finally Topper: Neha.

Expected output:
Neha, Imran, Asha, Ravi
Topper: Neha

Quick Quiz

Q1.

What does [1, 2, 3].map(n => n * 10) return?

Q2.

Which method keeps only the items that pass a test?

Q3.

What is printed? console.log([10, 9, 100].sort())

Q4.

How do you read the city property of const s = { name: "Ravi", city: "Indore" }?

Q5.

What does const { name, city } = student; do?