JavaScript Objects & Arrays: Storing Real Data Without Losing It
Group related fields into JavaScript objects, keep ordered lists in arrays, and transform data cleanly with .filter() and .map().
You'll learn how JavaScript objects group named properties together, how arrays store ordered lists, and how .filter() and .map() turn raw data into clean UI lists without mutating your original data. Assumes basic const/let knowledge.
1. Real data comes in groups, not loose variables
If you are building a todo app or a user profile, keeping userName, userEmail, and userLevel in separate loose variables falls apart the second you have two users.
JavaScript gives you two containers for real data:
- Objects
{ ... }— group named properties describing one thing (a user, a product, a post). - Arrays
[ ... ]— keep an ordered list of many things (all users, all posts, all todos).
const user = {
name: "Gerald",
role: "Night Shift Engineer",
coffees: 4,
};
console.log(user.name, "-> coffees:", user.coffees);Gerald -> coffees: 42. Reading and updating objects
You read a property with a dot (user.name), or with brackets (user["name"]) if the key name is stored in a variable.
Here is a subtle thing that trips people up: const user = { ... } prevents you from reassigning user = somethingElse, but it does not freeze the properties inside:
user.coffees += 1;
user.awake = false;
console.log(user);{ name: 'Gerald', role: 'Night Shift Engineer', coffees: 5, awake: false }If you read a property that does not exist (user.motivation), JavaScript does not crash—it quietly gives you undefined.
3. Arrays: ordered lists starting at zero
Arrays hold items in order, counted starting from 0:
const tasks = ["Fix bug", "Create two new bugs", "Blame cache"];
console.log(tasks[0]);
console.log(tasks.length);Fix bug
3To add an item to the end, use tasks.push("Go home"). To check the last item without knowing the length, use tasks.at(-1).
Create an array of 3 objects (e.g. const todos = [{ title: "...", done: false }, ...]) and log todos[0].title in your browser Console.
4. Transforming lists with .filter() and .map()
In modern JavaScript (and React), you rarely rewrite an array in place. Instead, you create a new filtered or transformed array:
const todos = [
{ title: "Drink water", done: true },
{ title: "Fix CSS", done: false },
{ title: "Sleep", done: false },
];
const remaining = todos
.filter((t) => !t.done)
.map((t) => t.title);
console.log(remaining);[ 'Fix CSS', 'Sleep' ].filter((t) => !t.done)keeps only items where the function returnstrue..map((t) => t.title)turns each object into just its title string.todositself is completely unchanged!
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
Used .map((t) => { t.title }) with braces and no return | Produces [undefined, undefined, undefined] | Omit braces (t) => t.title or add return |
Checked arr[arr.length] for the last item | Returns undefined (off by one) | Use arr[arr.length - 1] or arr.at(-1) |
Tried obj1 === obj2 to compare contents | Always false for two separate {} objects | Compare specific properties like obj1.id === obj2.id |
Practice
- Given
const nums = [2, 5, 8], what doesnums.filter((n) => n > 4).map((n) => n * 2)return? - What does
const item = { name: "Mug" }; console.log(item.price);print?
Answers:
[10, 16].undefined.
What you learned
- Objects
{ key: value }describe one thing with named properties. - Arrays
[a, b, c]hold ordered lists starting at index0. .filter()selects matching items and.map()transforms each item, leaving the original array untouched.