Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over a JavaScript object?

I have an object in JavaScript:

{     abc: '...',     bca: '...',     zzz: '...',     xxx: '...',     ccc: '...',     // ... } 

I want to use a for loop to get its properties. And I want to iterate it in parts (not all object properties at once).

With a simple array I can do it with a standard for loop:

for (i = 0; i < 100; i++) { ... } // first part for (i = 100; i < 300; i++) { ... } // second for (i = 300; i < arr.length; i++) { ... } // last 

But how to do it with objects?

like image 706
nkuhta Avatar asked Jan 17 '13 12:01

nkuhta


People also ask

How do I iterate over an object key?

You have to pass the object you want to iterate, and the JavaScript Object. keys() method will return an array comprising all keys or property names. Then, you can iterate through that array and fetch the value of each property utilizing an array looping method such as the JavaScript forEach() loop.

How do I iterate an object in node JS?

Since Javascript 1.7 there is an Iterator object, which allows this: var a={a:1,b:2,c:3}; var it=Iterator(a); function iterate(){ try { console. log(it. next()); setTimeout(iterate,1000); }catch (err if err instanceof StopIteration) { console.

Can I use forEach on an object?

JavaScript's Array#forEach() function lets you iterate over an array, but not over an object. But you can iterate over a JavaScript object using forEach() if you transform the object into an array first, using Object. keys() , Object. values() , or Object.


2 Answers

For most objects, use for .. in :

for (let key in yourobject) {   console.log(key, yourobject[key]); } 

With ES6, if you need both keys and values simultaneously, do

for (let [key, value] of Object.entries(yourobject)) {     console.log(key, value); } 

To avoid logging inherited properties, check with hasOwnProperty :

for (let key in yourobject) {    if (yourobject.hasOwnProperty(key)) {       console.log(key, yourobject[key]);    } } 

You don't need to check hasOwnProperty when iterating on keys if you're using a simple object (for example one you made yourself with {}).

This MDN documentation explains more generally how to deal with objects and their properties.

If you want to do it "in chunks", the best is to extract the keys in an array. As the order isn't guaranteed, this is the proper way. In modern browsers, you can use

let keys = Object.keys(yourobject); 

To be more compatible, you'd better do this :

 let keys = [];  for (let key in yourobject) {            if (yourobject.hasOwnProperty(key)) keys.push(key);  } 

Then you can iterate on your properties by index: yourobject[keys[i]] :

for (let i=300; i < keys.length && i < 600; i++) {     console.log(keys[i], yourobject[keys[i]]); } 
like image 134
Denys Séguret Avatar answered Sep 29 '22 13:09

Denys Séguret


Here is another iteration solution for modern browsers:

Object.keys(obj)   .filter((k, i) => i >= 100 && i < 300)   .forEach(k => console.log(obj[k])); 

Or without the filter function:

Object.keys(obj).forEach((k, i) => {     if (i >= 100 && i < 300) {         console.log(obj[k]);     } }); 

However you must consider that properties in JavaScript object are not sorted, i.e. have no order.

like image 33
VisioN Avatar answered Sep 29 '22 13:09

VisioN