Model collections, preserve source data and explain every transformation.
๐ Arrays๐๏ธ Objects๐ Data logic๐งช Lab + MCQs
01
๐ฏLearning Objectives
Level 14 calculated one workshopโs remaining seats.
Real pages often manage a collection of workshops.
This lesson groups values into arrays and objects, derives useful fields, selects matching records, orders the results and calculates a summary.
You will explain which methods return new arrays, which operations mutate data, and why a new outer array does not necessarily contain newly copied objects.
You will also test empty collections, full workshops and equal sort values instead of assuming every collection has a matching first item.
๐
Collections
Use array positions and iteration deliberately.
๐๏ธ
Records
Give related properties meaningful names.
๐
Transformations
Distinguish map, filter, find and reduce.
๐
Verification
Check source preservation, no-match results and tie behavior.
02
๐บ๏ธModel the Task Before Choosing a Method
Our task is a workshop catalog.
Each record needs a stable ID, title, capacity and reserved count.
The collection has an order, and a learner may search it or choose only workshops with available places.
A recordโs identity should not depend on its current display position.
Use an array when a sequence or collection is useful.
Use an object to group named properties of one record.
An array of objects combines these roles without forcing capacity, title and reserved values into several separate arrays that must stay synchronized.
State the data contract before calculating: capacity and reserved are small non-negative integers, reserved does not exceed capacity, and each workshop has a unique ID.
This lessonโs lab supplies trusted, predefined records; a real external data source would need its own validation.
One catalog, three levels
๐Array
The ordered collection of workshops.
๐๏ธObject
One workshop record with named fields.
๐ท๏ธProperty
A value such as title, capacity or reserved.
03
๐Arrays โ Positions, Length and Access
An array is an object designed for indexed collections.
Ordinary array indexes start at zero.
An array with three consecutive items has indexes 0, 1 and 2, while its length is 3.
The final index in that dense array is length - 1.
Reading a missing ordinary index produces undefined.
It does not magically create a valid workshop.
Checking the collection length or finding a record explicitly is safer than assuming records[0] exists.
Length and the number of explicitly present elements can differ for sparse arrays.
Our examples use dense arrays to avoid hiding a beginner logic error behind empty slots.
Avoid assigning far beyond the current end just to represent a workshop ID.
Write down both the return value and the array after the method call.
Do not write const topics = topics.push(...) and expect push to return the collection.
05
๐Iteration โ Indexes or Values?
A traditional for loop is useful when the index is part of the task.
For...of visits values from an iterable such as an array.
For...in enumerates property keys and can include inherited enumerable properties; it is not the default choice for reading array values.
ForEach calls a callback for present array elements and returns undefined.
It is suitable for a deliberate side effect, such as logging.
It does not build the transformed output array that map builds, and a return inside its callback does not break the surrounding iteration.
For these synchronous beginner examples, a for...of loop can use break when the task should stop.
Do not assume an asynchronous callback supplied to forEach will be awaited in sequence.
const topics = ["HTML", "CSS", "JavaScript"];
for (let index = 0; index < topics.length; index += 1) {
console.log(index, topics[index]);
}
for (const topic of topics) {
console.log(topic);
}
The first loop exposes both index and value.
The second directly names the value.
Choose the clearer version for the job instead of using an index when it contributes nothing.
06
๐๏ธObject Literals and Named Properties
An object literal groups related fields.
Dot notation reads a property with a suitable literal name.
Bracket notation can use a variable key or a property name that does not fit ordinary dot notation.
Missing properties generally read as undefined unless inheritance supplies them.
Property names and values have separate roles.
In workshop.title, title names the field; "HTML" is the stored value.
Keep numeric fields as numbers after validation rather than storing display strings such as "30 seats" and expecting arithmetic to work.
Use a stable ID when matching or updating records.
A title can change and two titles can be the same.
The array index can change when the collection is filtered or sorted.
Assigning workshop.reserved = 15 changes that property on the referenced object.
Object.keys and Object.entries can help inspect an objectโs own enumerable string-keyed fields.
They do not promise to expose every inherited or non-enumerable property.
An object can contain a function-valued property, often used as a method.
A regular method can access this when called with the expected receiver.
This depends on how a function is invoked; it is not permanently the name of the object where the function was first written.
Arrow functions do not create their own this binding.
For a beginner catalog calculation, a pure helper that takes a record argument is usually easier to test than relying on an invocation-dependent receiver.
Follow the normal dataset through a fixed available-workshop query. Use the established numbered timeline and playback controls to inspect the role of each method. The source data stays separate from the displayed results.
JAVASCRIPT COLLECTION TRACE
Step 1 of 5
๐
STEP 01
Start with four workshop records
The fixed catalog contains HTML, CSS, JavaScript and Accessibility.
capacity / reserved: 30/12, 20/20, 40/10, 12/0
What is happening?
The predefined records meet the data contract. Preserve their original values and source order.
17
๐งชPremium Interactive โ Catalog Logic Lab
Choose a normal catalog, an empty catalog, an all-full catalog or equal-seat records.
Search the title, select an availability filter and choose the result order.
The lab executes fixed array/object operations on trusted predefined data; it does not evaluate arbitrary code or accept an external dataset.
Inspect the original records, the selected derived records, their title list and the summary.
A no-match selection is reported explicitly.
The raw every-full result is true for an empty selection and is explained rather than presented as proof that workshops exist.
Equal remaining counts use the ID as a tie rule.
The fixed titles use simple lowercase comparison; this lab is not a general locale-aware sorting service.
Each run begins from the chosen source dataset, so one run does not silently mutate the next runโs input.
Ready to select available workshops.
Original source records
Operations applied
Selected records and summary
18
๐ ๏ธDebugging โ Inspect Shape, Return and Mutation
๐
Shape
Confirm an actual array and the required record fields.
โฉ๏ธ
Return
Check that callbacks return their mapped value or predicate decision.
๐งต
Mutation
Inspect both the outer array and referenced objects.
๐ญ
Empty case
Check length, find results and reduction initial values.
If map yields undefined entries, inspect the callbackโs return path.
If filter returns no records, inspect the predicate and the data types it compares.
If a numeric sort gives 10 before 2, check whether a numeric comparator is missing.
If an empty result crashes, look for a missing initial accumulator or unchecked records[0].
A changed source title after a copy can indicate shared item references.
A changed source order can indicate sort was called directly on the source.
Use targeted snapshots or known assertions to inspect the actual contract, rather than assuming every method that returns an array is deeply non-mutating.
For this lab, normal/available/descending yields three records and a total of 60.
Empty/all yields zero records and total zero.
Full/available yields no matches.
Ties/available/descending keeps the ID tie rule explicit.
19
๐ฌInterview Questions โ Flip to Explain
QUESTION
What is the difference between an array and a record object?
Click or press Enter to explain
ANSWER
An array organizes an indexed collection; an object groups named properties. An array of objects can represent a catalog.
QUESTION
Does const prevent push on an array?
Click or press Enter to explain
ANSWER
No. Const prevents reassignment of the binding, not allowed changes to the array it references.
QUESTION
How do slice and splice differ?
Click or press Enter to explain
ANSWER
Slice creates a shallow range copy. Splice changes its receiver and returns the removed items.
QUESTION
What is the difference between map and filter?
Click or press Enter to explain
ANSWER
Map derives output values for present input elements. Filter selects existing elements whose predicate passes.
QUESTION
What does find return with no match?
Click or press Enter to explain
ANSWER
Undefined. Check that result before accessing properties.
QUESTION
Why does every return true for an empty array?
Click or press Enter to explain
ANSWER
There is no present item that fails the predicate. Combine it with a non-empty check when the task requires records to exist.
QUESTION
Why copy before calling sort?
Click or press Enter to explain
ANSWER
Sort mutates its receiver. Sorting a copied outer array preserves the original array order, while item references may still be shared.
QUESTION
Why provide an initial value to reduce?
Click or press Enter to explain
ANSWER
It defines the accumulatorโs starting value and supports an empty collection. For seat totals, initial zero gives an empty total of zero.
20
โMCQ Practice โ Explain the Collection
PRACTICE
1. What is the first ordinary array index?
PRACTICE
2. What does push return?
PRACTICE
3. Which method creates a shallow range copy without removing source items?
PRACTICE
4. Which loop directly visits array values?
PRACTICE
5. What does map produce when this dense input callback returns a derived record?
PRACTICE
6. What does find return when no record matches?
PRACTICE
7. What does an empty arrayโs every predicate test return?
PRACTICE
8. Which initial value fits a reduce calculation of total remaining seats?
PRACTICE
9. What does default sort do to [2, 10, 3]?
PRACTICE
10. Does [...records] deeply clone every nested record?
PRACTICE
11. What should a no-match filtered result be reported as?
PRACTICE
12. Which total belongs to normal/available in this lesson?
21
๐ปExtra Practice โ Test Specific Collection Cases
๐
Normal query
Select normal/available/most first. Expected titles: JavaScript, HTML, Accessibility; total 60.
๐ญ
Empty source
Select empty/all. Expected: zero records, total zero, some available false, raw every full true.
๐ซ
No availability
Select full/available. Expected: no matching records; do not read a nonexistent first title.
๐
Search phrase
Use normal/all and search HTML. Expected: one HTML record, remaining total 18.
๐งต
No match
Search a phrase absent from every title. Expected: an empty selection and clear explanation.
โ๏ธ
Tie rule
Select ties/available/most first. Expected: IDs t1, t2, t3; each has 10 remaining, total 30.
โ๏ธ
Copy experiment
Copy an array containing one object, then edit the copied item title.
Expected: shared item changes the original until you copy that object too.
๐งฎ
Reduce comparison
Total [18, 0, 30, 12] with a loop and reduce initial zero. Expected: both yield 60; empty input yields zero.
22
๐Quick Revision
๐Array: Use zero-based positions and distinguish length from an ID.
๐๏ธObject: Group named fields and preserve stable identity.
โ๏ธMutation: Know both the return value and the changed receiver.
๐งตCopies: New outer arrays can share referenced items.
๐map: Return a derived value for each present item.
๐งฒfilter/find: Distinguish a selected array from a first match.
โ some/every: Explain the empty-case boolean results.
๐งฎreduce: Choose an initial accumulator that fits the task.
โ๏ธsort: Copy first, use a comparator and state ties.
๐Pipeline: Summarize the selection actually displayed.
23
๐Glossary โ Flip to Learn
TERM
Array
Click to see meaning
DEFINITION
Array
An object used for indexed collections and array operations.
TERM
Index
Click to see meaning
DEFINITION
Index
A position key used to access an array item, commonly starting at zero.
TERM
Property
Click to see meaning
DEFINITION
Property
A named or keyed field on an object.
TERM
Reference
Click to see meaning
DEFINITION
Reference
An association with an object that can be shared by multiple bindings.
TERM
Mutation
Click to see meaning
DEFINITION
Mutation
A change to an existing object or array.
TERM
Shallow copy
Click to see meaning
DEFINITION
Shallow copy
A new outer container whose nested object references may remain shared.
TERM
Callback
Click to see meaning
DEFINITION
Callback
A function passed to another operation to be called by it.
TERM
Predicate
Click to see meaning
DEFINITION
Predicate
A function or expression used to decide whether a condition passes.
TERM
Accumulator
Click to see meaning
DEFINITION
Accumulator
The carried summary value updated during a reduction.
TERM
Comparator
Click to see meaning
DEFINITION
Comparator
A function that defines how two values should be ordered for sorting.
24
๐Final Challenge โ A Catalog Query Function
Write a pure query function that accepts a trusted flat workshop collection and options for title search, availability and order.
Return derived selected records, their title list, a count and total remaining seats.
Preserve the source fields and order.
State the required input contract.
Use map to create new derived record objects, filter for the query, and copy before sort.
Choose an explicit tie rule.
Provide an initial zero to reduction.
Check a missing find result before reading it, and keep an empty query result distinct from an invalid source record.
Test the four predefined scenarios, a title match and a no-match phrase.
Snapshot the input before a query and compare it afterward.
Verify 60 selected remaining seats for the normal available query, zero for an empty selection and 30 for the tie dataset.
Explain the raw empty every result in plain language.
Success criterion: explain each methodโs input and return shape, prove source preservation for these flat records and report empty results clearly.
Level 16 will connect these data results to DOM and browser programming.
25
โ Level 15 Complete?
Before marking complete, derive and select a catalog, sort a copy, summarize an empty collection and explain a shallow-copy limitation. Completion is a local study marker rather than an assessment score.