Javascript Array values()

Introduction

Array values() will return an iterator of the array's elements.

arr.values()
const a = ["foo", "bar", "baz", "qux"];
const aValues = Array.from(a.values());
console.log(aValues);//  w w w . jav  a  2 s.c  o m

Iteration using for...of loop

var arr = ['a', 'b', 'c', 'd', 'e'];
var iterator = arr.values();

for (let letter of iterator) {
  console.log(letter);/*from   www . j  a  v  a2  s . co m*/
}  //"a" "b" "c" "d"

Iteration using .next()

var arr = ['a', 'b', 'c', 'd', 'e'];
var iterator = arr.values(); 
console.log(iterator.next());               
console.log(iterator.next().value);         
console.log(iterator.next()["value"]);      
console.log(iterator.next());               
console.log(iterator.next());               
console.log(iterator.next());               
console.log(iteraror.next().value);    // ww  w.java2 s  . c o m



PreviousNext

Related