mirror of
https://github.com/Fechin/reference.git
synced 2026-11-20 21:23:11 +03:00
Merge pull request #722 from iamashruu/patch-1
Add for...in loop examples for array iteration in javascript cheatsheet
This commit is contained in:
1 file changed
+19
@@ -742,12 +742,31 @@ for (let i = 0; i < 2; i += 1) {
|
||||
```javascript
|
||||
const fruits = ['apple', 'orange', 'banana'];
|
||||
|
||||
// 1. Print only indexes
|
||||
for (let index in fruits) {
|
||||
console.log(index);
|
||||
}
|
||||
// => 0
|
||||
// => 1
|
||||
// => 2
|
||||
|
||||
|
||||
// 2. Print only values
|
||||
for (let index in fruits) {
|
||||
console.log(fruits[index]);
|
||||
}
|
||||
// => apple
|
||||
// => orange
|
||||
// => banana
|
||||
|
||||
|
||||
// 3. Print index with value
|
||||
for (let index in fruits) {
|
||||
console.log(index, fruits[index]);
|
||||
}
|
||||
// => 0 apple
|
||||
// => 1 orange
|
||||
// => 2 banana
|
||||
```
|
||||
|
||||
### for...of loop
|
||||
|
||||
Reference in new issue
Block a user