Lesson 17 / الدرس 17

Objects: values with names / الكائنات: قيم بأسماء

An array numbers its values; an object names them. Once a thing has more than two properties, an object is what stops you remembering which position the price was in.

المصفوفة ترقّم قيمها؛ والكائن يسمّيها. وما إن يصير للشيء أكثر من خاصيتين، حتى يكون الكائن هو ما يمنعك من تذكّر أي موضع كان فيه السعر.

const product = {
  name: 'Dates',
  price: 120,
  inStock: true
};

product.name          'Dates'      the usual way
product['name']       'Dates'      when the key is in a variable
product.colour        undefined    no error for a missing one
product.price = 130                change it
product.weight = '1kg'             add a new one
Each name is a key and each value is a value. Asking for a key that does not exist gives undefined rather than an error — the same quiet behaviour as an array position that is not there. Run the demonstration.

A list of objects is what real data looks like

Almost everything a program handles arrives in this shape: an array of objects, each with the same keys. A basket, a list of students, the results of a search, and the JSON of lesson 20 in the web course — all the same. Everything from lesson 16 works on it directly, which is why those two lessons are next to each other.

Reading, changing and copying

To do thisWrite
Get a valueproduct.price
Get one by a name in a variableproduct[key]
Add or changeproduct.weight = "1kg"
Removedelete product.weight
Does it have this key"price" in product
All the keysObject.keys(product)
All the valuesObject.values(product)
Copy it{ ...product }

The last row is worth learning now. const copy = product does not copy — both names point at the same object, and changing one changes the other. { ...product } makes an actual copy.

An object can hold anything, including arrays and other objects, and real data nests: a customer has an address, which has a city. Nesting more than two or three levels deep usually means the shape is fighting you, and flattening it — or naming the inner piece as its own variable before working with it — makes everything downstream shorter.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. What does const copy = product do?

2. What does order.customer?.email do when there is no customer?

3. What shape does real program data usually take?

Your task / مهمتك

Model something real as an array of objects — a class of students with marks, a menu with prices and categories, your own bookshelf. Give each at least four keys including one boolean. Then answer four questions about it using the methods from lesson 16, print a formatted table of the whole list, and demonstrate the copy trap: change a value through a second name and show that the original changed too.

مثّل شيئًا حقيقيًا مصفوفةَ كائنات — صفًا من الطلاب بدرجات، أو قائمة طعام بأسعار وفئات، أو رفّ كتبك. وأعطِ كل واحد أربعة مفاتيح على الأقل بينها قيمة منطقية. ثم أجب أربعة أسئلة عنها بدوال الدرس 16، واطبع جدولًا منسّقًا للقائمة كلها، واعرض فخّ النسخ: غيّر قيمة عبر اسم ثانٍ وأظهر أن الأصل تغيّر أيضًا.

  • A real list of objects, four keys each قائمة كائنات حقيقية بأربعة مفاتيح لكل واحد
  • Four questions answered with array methods أربعة أسئلة مُجابة بدوال المصفوفات
  • The whole list printed as a table القائمة كلها مطبوعة جدولًا
  • The copy trap demonstrated, then fixed فخ النسخ معروضًا ثم مُصلحًا
How do you want to submit? / كيف تريد التسليم؟