1

For some reason it's returning both the key and value an excessive number of times. What am I doing wrong?

function loopAnArrayOfObjects(arrayOfObjects) {
 for (var i = 0; i < arrayOfObjects.length; i++) {
 for (var key in arrayOfObjects) {
 console.log(arrayOfObjects[key]);
 }
 }
}
loopAnArrayOfObjects([{a: 1, b: 2}, {z: 5, y: 6}, {q: 14} ]);
// expected console output:
 // 1
 // 2
 // 5
 // 6
 // 14
loopAnArrayOfObjects([{queue: false, stack: true}, {fish: 'swims'}, {shirt: 's', pop: 'p', eye: 'e'} ]);
// expected console output:
 // false
 // true
 // swims
 // s
 // p
 // e

Unmitigated
91.5k12 gold badges103 silver badges109 bronze badges
asked Feb 23, 2021 at 21:42
3
  • For the inner loop, did you mean for (var key in arrayOfObjects[i]) { console.log(arrayOfObjects[i][key]);? Commented Feb 23, 2021 at 21:47
  • Is the expected out or the actual output in the comments? Commented Feb 23, 2021 at 21:48
  • I'll put this as a comment, but you can use .reduce() to remove the counters too: [{a: 1, b: 2}, {z: 5, y: 6}, {q: 14} ].reduce((acc,next)=>{for(key in next) acc.push(next[key]); return acc;},[]) Commented Feb 23, 2021 at 21:58

2 Answers 2

3

In each iteration, the current element/object is arrayOfObjects[i], which is what you should be using.

function loopAnArrayOfObjects(arrayOfObjects) {
 for (var i = 0; i < arrayOfObjects.length; i++) {
 for (var key in arrayOfObjects[i]) {
 console.log(arrayOfObjects[i][key]);
 }
 }
}
loopAnArrayOfObjects([{queue: false, stack: true}, {fish: 'swims'}, {shirt: 's', pop: 'p', eye: 'e'} ]);

Alternatively, you can use for ... of to loop over just the elements without the index.

function loopAnArrayOfObjects(arrayOfObjects) {
 for(const obj of arrayOfObjects){
 for (const key in obj) {
 console.log(obj[key]);
 }
 }
}
loopAnArrayOfObjects([{queue: false, stack: true}, {fish: 'swims'}, {shirt: 's', pop: 'p', eye: 'e'} ]);

answered Feb 23, 2021 at 21:53
Sign up to request clarification or add additional context in comments.

1 Comment

This answer actually answers the question "What did I do wrong."
0

Since you are looping over arrays, it's best these days to do it with one of the Array.prototype methods. .forEach() is a great replacement for traditional counting loops because it does away with the need to set up and manage a counter and instead gives you direct access to the elements within the array. This simplification can often remove closures that will often cause problems or just make it easier to understand what you are working with. Then, when needing to loop over the object keys, you can use a for/in loop to also make it much simpler.

loopAnArrayOfObjects([{a: 1, b: 2}, {z: 5, y: 6}, {q: 14} ]);
// console output:
 // 1
 // 2
 // 5
 // 6
 // 14
loopAnArrayOfObjects([{queue: false, stack: true}, {fish: 'swims'}, {shirt: 's', pop: 'p', eye: 'e'} ]);
// console output:
 // false
 // true
 // swims
 // s
 // p
 // e
 
function loopAnArrayOfObjects(ary){
 ary.forEach(function(arrayItem){
 // This will be the objects in the array
 // Now, set up a secondary loop to go over the keys in the object
 for(key in arrayItem){
 console.log(arrayItem[key]);
 }
 });
}

answered Feb 23, 2021 at 21:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.