Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to loop through a set of elements in JavaScript

3 Answers

0 votes
const elements = [{a:1,b:2}, {c:3,d:4}, {e:7,f:3}, {g:4,h:9}, {i:5,j:0}];

for (let i = 0; i < elements.length; i++) {
    const item = elements[i];
    console.log("index", i, "item:", item); 
}
 

 
    
/*
run:

index 0 item: { a: 1, b: 2 }
index 1 item: { c: 3, d: 4 }
index 2 item: { e: 7, f: 3 }
index 3 item: { g: 4, h: 9 }
index 4 item: { i: 5, j: 0 }

*/
 
 

 



answered Mar 3, 2025 by avibootz
0 votes
const elements = [{a:1,b:2}, {c:3,d:4}, {e:7,f:3}, {g:4,h:9}, {i:5,j:0}];

elements.forEach((item, index) => {
    console.log("index", index, "item:", item);
});

 
    
/*
run:

index 0 item: { a: 1, b: 2 }
index 1 item: { c: 3, d: 4 }
index 2 item: { e: 7, f: 3 }
index 3 item: { g: 4, h: 9 }
index 4 item: { i: 5, j: 0 }

*/
 
 

 



answered Mar 3, 2025 by avibootz
0 votes
const elements = [{a:1,b:2}, {c:3,d:4}, {e:7,f:3}, {g:4,h:9}, {i:5,j:0}];

for (let i = 0; i < elements.length; i++) {
  const item = elements[i];
  for (const key in item) {
    if (item.hasOwnProperty(key)) {
      console.log(`${key}: ${item[key]}`);
    }
  }
}

    
/*
run:

a: 1
b: 2
c: 3
d: 4
e: 7
f: 3
g: 4
h: 9
i: 5
j: 0

*/
 
 

 



answered Mar 3, 2025 by avibootz

Related questions

2 answers 140 views
1 answer 139 views
3 answers 191 views
4 answers 282 views
2 answers 171 views
3 answers 211 views
...