I am seeing a weird issue in a Lightning Community with an Aura Component.

When I loop over this structure:

    "label": "Contact (OrderApi__Contact__c)",
    "value": "OrderApi__Contact__c"
    "label": "Billing City (OrderApi__Billing_City__c)",
    "value": "OrderApi__Billing_City__c"
    "label": "Billing State (OrderApi__Billing_State__c)",
    "value": "OrderApi__Billing_State__c"
    "label": "Certification (FON_Certification__c)",
    "value": "FON_Certification__c"
    "label": "Activated Date (OrderApi__Activated_Date__c)",
    "value": "OrderApi__Activated_Date__c"
    "label": "Paid Through Date (OrderApi__Paid_Through_Date__c)",
    "value": "OrderApi__Paid_Through_Date__c"

Using for ... in, I am getting these keys:

for (var index in columnHeadersArray) {

The sum key is throwing an error because when I try to access an Array index, it throws an error. Any reason why I would be getting a sum key?

  • 2
    Only if somewhere in your code there is something equivalent to columnHeadersArray['sum'] = .... – Keith C Sep 18 at 13:16
  • columnHeadersArray["sum"] does return some function, but I dont see that in the Object I am iterating over – Bryan Anderson Sep 18 at 13:19

Don't use for ... in for this, use an Array.prototype method, such as:

columnHeadersArray.forEach((value) => { console.log(value); }) // output elements


columnHeadersArray.map((value) => value.label); // Makes an array of labels

Etc. Check out the MDN documentation for the varied methods you can use on an Array or Array-like object.

If the object is actually only like an Array, but not a real Array, you can call the method instead:

Array.prototype.forEach.call(columnHeadersArray, (value) => { console.log(value); });

See also this Q&A on Stack Overflow about why you should not use for...in to iterate over an Array or Array-like object.

| improve this answer | |
  • Thanks @sfdcfox, will do. So you have no idea thought a random sum function would be introduced though? – Bryan Anderson Sep 18 at 13:49
  • @BryanAnderson The Q&A I linked hints at this, but to be more precise, for...in loops over all the keys in the object, including any inherited by prototype. In Aura/LWC, most Array methods are overridden to provide Locker Service filtering on data. These methods will then appear in the prototype chain, thus being enumerated in for...in. Presumably, this sum function is a Locker Service variant of the Array.prototype.sum method. – sfdcfox Sep 18 at 13:54
  • Oh wow @sfdcfox, thank you very much – Bryan Anderson Sep 18 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.