JavaScript・Grouping and repeating

Objects: bundling values under names

If an array is a "numbered shelf," an object is a "labeled drawer." Different kinds of information—name, favorites, age—held together under one name.

An array was a “numbered shelf.” But when you want to hold a name, a place, and a favorite thing—different kinds of information—managing them by number gets hard. That’s where the object comes in: a drawer where every value carries a name label.

Why an array is awkward here

const shima = ["Shima", "Snowland", "drawing"];
console.log(shima[1]);

Nothing about shima[1] tells you what it holds. With an object, you can write:

const shima = {
  name: "Shima",
  from: "Snowland",
  like: "drawing"
};
console.log(shima.from);

shima.from—you can read it and know it’s where they live.

The rules for writing one

const shima = {
  name: "Shima",
  age: 3
};
PartMeaning
{ }The container for the whole drawer (an array’s [ ])
nameThe key—the label’s name
:The separator between label and contents
"Shima"The value—the contents
,The separator before the next label

You choose the key names yourself. Values can be text, numbers, even arrays.

Reading and changing

To read, join them with a dot.

console.log(shima.name);
console.log(shima.age);

To change, use the same shape with an =.

shima.age = 4;
console.log(shima.age);

You can also add a new label later.

shima.hobby = "walks";

As with arrays, an object made with const can still have its contents changed (what’s forbidden is swapping the whole drawer for a different one).

Array or object?

ArrayObject
Shape["A", "B", "C"]{ name: "A", age: 3 }
How you read itBy number ([0])By label (.name)
Good forThe same kind of thing, repeatedDifferent facts about one thing
ExampleA list of favorites, a list of scoresA profile, settings, one product

When unsure, ask whether the order carries meaning. If it does, use an array; if you’d rather call things by name, use an object.

They combine nicely

An object can hold an array:

const shima = {
  name: "Shima",
  likes: ["drawing", "ramen", "games"]
};

console.log(shima.likes[0]);
console.log(shima.likes.length);

shima.likes gets you the shelf, then [0] takes one off it—read it left to right and it makes sense.

One object, and we pull out only the values we name.
JavaScriptArrays: bundling many values togetherArrays, the counterpart that bundles by number

Summary of this lesson

  1. An object is { key: value, … }—a labeled drawer
  2. Read with name.key. Changing and adding use the same shape
  3. Repeated things of one kind go in an array; facts about one thing go in an object

Try it

The “objects” you learned today are not used on Shima’s page (the profile is written directly in the HTML). They’re a tool for handling lots of data in a program—for pages that display data fetched from elsewhere. Keep them in mind and use them when you need them.

FAQ

When should I use an array and when an object?
Use an array when the same kind of thing repeats (three favorites, a list of scores), and an object when you're bundling different facts about one thing (name, age, where they live).
How do I read a value out of an object?
Write a dot and the key name after the object name (shima.name). If the key is held in a variable, square brackets work too (shima["name"]).
What happens if I ask for a key that doesn't exist?
You get undefined rather than an error. A typo in the key name gives the same result, so check your spelling first when a value doesn't appear.