User Tools

Site Tools


java-script:for-in-vs-for-of

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
java-script:for-in-vs-for-of [2023/08/10 03:01] odeftajava-script:for-in-vs-for-of [2023/08/11 12:22] (current) odefta
Line 18: Line 18:
 </code> </code>
  
-==== Example with an array ====+==== Example with an Array ====
 <code javascript> <code javascript>
 const arr = [10, 20, 30]; const arr = [10, 20, 30];
Line 58: Line 58:
 It can't be used directly on plain objects without using methods like Object.keys, Object.values, or Object.entries. It can't be used directly on plain objects without using methods like Object.keys, Object.values, or Object.entries.
 </note> </note>
-Example with an Array:+ 
 +==== Example with an Array ==== 
 <code javascript> <code javascript>
 const arr = [10, 20, 30]; const arr = [10, 20, 30];
Line 73: Line 75:
 However, you can convert an object's keys, values, or key-value pairs into an array and then iterate over that array with a for...of loop. \\  However, you can convert an object's keys, values, or key-value pairs into an array and then iterate over that array with a for...of loop. \\ 
 Example: Example:
-<code>+<code javascript>
 const person = { const person = {
   name: 'Alice',   name: 'Alice',
Line 109: Line 111:
 In this example, **Object.keys(person)**, **Object.values(person**), and **Object.entries(person)** return arrays of the object's keys, values, and key-value pairs, respectively, allowing you to iterate over them with a **for...of** loop. In this example, **Object.keys(person)**, **Object.values(person**), and **Object.entries(person)** return arrays of the object's keys, values, and key-value pairs, respectively, allowing you to iterate over them with a **for...of** loop.
  
-====== Conclusion ======+===== Conclusion =====
  
   * Use **for...in** to iterate over the **keys** of an **object** (including non-array objects).   * Use **for...in** to iterate over the **keys** of an **object** (including non-array objects).
java-script/for-in-vs-for-of.1691625695.txt.gz · Last modified: 2023/08/10 03:01 by odefta