freeCodeCamp/guide/arabic/certifications/javascript-algorithms-and-d.../basic-data-structures/-iterate-through-the-keys-o.../index.md

1.3 KiB

title localeTitle
Iterate Through the Keys of an Object with a for...in Statement  يتكرر عبر مفاتيح كائن مع لـ ... في بيان

يتكرر عبر مفاتيح كائن مع لـ ... في بيان

طريقة:

  • ملاحظة: ستتسبب dot-notation حدوث أخطاء في هذا التحدي.
  • يجب استخدام الترميز [square-bracket] لاستدعاء اسم خاصية متغير.
  • التعليمة البرمجية التالية لن تعمل.

مثال 1:

for (let user in obj) { if(obj.user.online === true) { //code } }

  • يوضح المثال 2 كيفية استخدام الترميز [square-bracket] في تنفيذ الشفرة.

المثال 2:

for (let user in obj) { if(obj[user]online === true) { //code } }

حل:

let users = { Alan: { age: 27, online: false }, Jeff: { age: 32, online: true }, Sarah: { age: 48, online: false }, Ryan: { age: 19, online: true } }; function countOnline(obj) { // change code below this line let result = 0; for (let user in obj) { if(obj[user].online === true) { result++; } } return result; // change code above this line } console.log(countOnline(users));