简体   繁体   中英

Iterate over a List object in Javascript

How can I iterate over a List object in Javascript? It contain more than 1 value.

 List <controllerclass> obj = dao.listimpl();

I tried this:

 for (index = 0; index < obj .length; ++index) {
        console.log(obj [index]);
    }

Object does not have length property like array. Please use the following code to iterate

 for (var key in obj) {
          if (obj.hasOwnProperty(key)){

          alert("property  "+key+ " is " + obj[key] ); 
   }
  }

Hope this solves your problem.

First of all List <controllerclass> obj = dao.listimpl(); this is java code you have pasted, try to paste the exact object which you trying to iterate.

Though i would recommend use a very lightweight javascript library Loadash which is very handy, when you are dealing with array/objects and looping through these items and many more util methods.

_.forOwn(obj, function(value, key) { } );

Loadash - forOwn

Iterates over own enumerable string keyed properties of an object and invokes iteratee for each property. The iteratee is invoked with three arguments: (value, key, object). Iteratee functions may exit iteration early by explicitly returning false

Looping using Iterators in Javascript is new to ES6 and explained below.

MDN Iterators and generators - Explains that, in Javascript, an iterator is a data object that just has a next() method. The result returned from Iterator.next() has a next() method, done and value properties. An example is shown below using the Javascript Set object.

let aCollection = new Set(['item1', 'item2', 'item3']);
let it = aCollection[Symbol.iterator]();
console.log('iterator is', it)
console.log('it.done - is not meaningful', it.done); // notice that it.done is not defined!
let itLoop = it.next()
console.log('it.next()', itLoop);
console.log('it.next().value', itLoop.value);
console.log('it.next().done', itLoop.done);

Output of the above sample code is:

iterator is SetIterator { 'item1', 'item2', 'item3' }
it.done - is not meaningful undefined
it.next() { value: 'item1', done: false }
it.next().value item1
it.next().done false

Set.prototype@@iterator - Explains how to use a Set iterator. What puzzled me is why examples didn't include looping so I provide examples below.

Using for...of

The easiest way to loop using an iterator is using the for(item of collection) syntax as described here in MDN . A more complete description of looping is covered very well here .

for(let item of aCollection){
    console.log('for...of returns ', item);
}

Using for(;;)

Next is a for loop using an iterator.

for(let it = aCollection[Symbol.iterator](), loop = it.next(); ! loop.done; loop = it.next()){
    console.log('for(;;) ', loop.value);
}

Using while loop

And next is a while loop using an iterator:

let it = aCollection[Symbol.iterator]();
let loop = it.next();
while (! loop.done){
    console.log('while loop ', loop.value);
    loop = it.next();
}

Hopefully this helps someone. The full runable example is at https://jsfiddle.net/PatS2265/y5a4hvb7/4/ . There is no HTML or CSS, just Javascript console.log output, so open a browser console window see the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM