2

I am having weird issue. I wanted to replace for (let i=0;i<arr.length;i++) with for( const[key,val] of arr) but I am getting error .for is not iterable

When I check my array with Object.prototype.toString.call(arr) i get [object Array] I am not insisting on using that form, but just want to understand why is this happening: my array looks like (array of objects):

[
  {
    fieldname: 'files',
    originalname: 'User Manual 3.0.pdf',
    encoding: '7bit',
    mimetype: 'application/pdf',
    buffer: <Buffer 25 50 44 46 2d 31 2e 37 0d 0a 25 b5 b5 b5 b5 0d 688657 more bytes>,
    size: 688707
  }
]
0

2 Answers 2

4

for( const[key,val] of arr) is trying to use

  1. An iterator for arr (the for-of part), which is fine; and

  2. An iterator for each value produced by #1 (the [key, val] part), which isn't fine because those are non-iterable objects

If you want to loop through the properties of the objects in arr in key, value form, you'll need Object.entries:

for (const entry of arr) {
    for (const [key, value] of Object.entries(entry)) {
        // ...
    }
}

(Or for-in if you want to include inherited properties.)

Live Example:

const arr = [
  {
    fieldname: 'files',
    originalname: 'User Manual 3.0.pdf',
    encoding: '7bit',
    mimetype: 'application/pdf',
    /*
    buffer: <Buffer 25 50 44 46 2d 31 2e 37 0d 0a 25 b5 b5 b5 b5 0d 688657 more bytes>,
    */
    size: 688707
  }
];

for (const entry of arr) {
    for (const [key, value] of Object.entries(entry)) {
        console.log(key, value);
    }
}

If you only wanted specific properties from the objects in arr, you could use object destructuring rather than iterable destructuring:

for (const {fieldname, originalname} of arr) {
    console.log(`fieldname = ${fieldname}, originalname = ${originalname}`);
}

Live Example:

const arr = [
  {
    fieldname: 'files',
    originalname: 'User Manual 3.0.pdf',
    encoding: '7bit',
    mimetype: 'application/pdf',
    /*
    buffer: <Buffer 25 50 44 46 2d 31 2e 37 0d 0a 25 b5 b5 b5 b5 0d 688657 more bytes>,
    */
    size: 688707
  }
];

for (const {fieldname, originalname} of arr) {
    console.log(`fieldname = ${fieldname}, originalname = ${originalname}`);
}

Sign up to request clarification or add additional context in comments.

5 Comments

of course, I guess it's not for my use then, as I need that "key" value which is not a key but just iteration number, so I am going back to oldschool for loop. Thank you fir explanation, I will use that in the future
@uneasy - It wasn't the for-of loop that was the problem, it was the [key, val] destructuring. But yes, if you need the index into arr, for is probably your best bet.
if you want a 'key' you could consider forEach as well (your key being the index)
@malarres yes, except forEach is not async so that's a no go for me in that case.
uff if you have to await something inside then it's better to keep the tried and true for(;;) notation :-P
-1

You cannot directly access the JSON key value pair by iterating the array. As T.J mentioned, you need to iterate the array and then iterate the key values from the object.

1 Comment

Thanks for contributing! A couple of notes: There is no JSON in the question. JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. Also, there's no need to post an answer just saying "the other answer is correct." :-)

Your Answer

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.