Js para...en la serie

Ejemplos de código

21
0

javascript bucle a través de la matriz de

var data = [1, 2, 3, 4, 5, 6];

// traditional for loop
for(let i=0; i<=data.length; i++) {
  console.log(data[i])  // 1 2 3 4 5 6
}

// using for...of
for(let i of data) {
	console.log(i) // 1 2 3 4 5 6
}

// using for...in
for(let i in data) {
  	console.log(i) // Prints indices for array elements
	console.log(data[i]) // 1 2 3 4 5 6
}

// using forEach
data.forEach((i) => {
  console.log(i) // 1 2 3 4 5 6
})
// NOTE ->  forEach method is about 95% slower than the traditional for loop

// using map
data.map((i) => {
  console.log(i) // 1 2 3 4 5 6
})
8
0

js bucle a través del objeto

const obj = { a: 1, b: 2 };

Object.keys(obj).forEach(key => {
	console.log("key: ", key);
  	console.log("Value: ", obj[key]);
} );
8
0

js bucle de la matriz de

let colors = ['red', 'green', 'blue'];
for (const color of colors){
    console.log(color);
}
8
0

javascript para de

const array = ['hello', 'world', 'of', 'Corona'];

for (const item of array) {
  console.log(item);
}
7
0

js bucle a través del objeto

const obj = { a: 1, b: 2 };

Object.keys(obj).forEach(key => {
	console.log("key: ", key);
  	console.log("Value: ", obj[key]);
} );
7
0

javascript bucle a través de la matriz de

const myArray = ['foo', 'bar'];

myArray.forEach(x => console.log(x));

//or

for(let i = 0; i < myArray.length; i++) {
  console.log(myArray[i]);
}
6
0

javascript bucle a través de la matriz de

const myArray = ['foo', 'bar'];

myArray.forEach(x => console.log(x));

//or

for(let i = 0; i < myArray.length; i++) {
  console.log(myArray[i]);
}
6
0

N

const myArray = ['foo', 'bar'];

myArray.forEach(x => console.log(x));

//or

for(let i = 0; i < myArray.length; i++) {
  console.log(myArray[i]);
}
2
0

N

const array = [1, 2, 3];array.forEach(function(elem, index, array) {    array[index] = elem * 2;});console.log(array); // [2,4,6]
2
0

objeto de javascript de la matriz de iteración

let obj = {
  key1: "value1",
  key2: "value2",
  key3: "value3"
}

Object.keys(obj).forEach(key => {
  let value = obj[key];
  //use key and value here
});
1
0

para los arrays de javascript

let iterable = [10, 20, 30];

for (let value of iterable) {
  value += 1;
  console.log(value);
}
// 11
// 21
// 31
0
0

N

function* fibonacci() { // a generator function
  let [prev, curr] = [0, 1];
  while (true) {
    [prev, curr] = [curr, prev + curr];
    yield curr;
  }
}

for (const n of fibonacci()) {
  console.log(n);
  // truncate the sequence at 1000
  if (n >= 1000) {
    break;
  }
}
0
0

foreach objeto js

const object1 = {
  a: 'somestring',
  b: 42
};
for (let [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`);
}
// expected output:
// "a: somestring"
// "b: 42"
// order is not guaranteed
0
0

N

const iterable = [10, 20, 30];

for (const value of iterable) {
  console.log("fef"+value);
}
// 10
// 20
// 30

En otros idiomas

Esta página está en otros idiomas

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................