0

I was going through this blog to understand foreach loop, but the blog says that use of Array.map() is more efficient than for foreach. So, I tried implementing that in my Lightning Component, but it doesn't let me save and throws FIELD_INTEGRITY_EXCEPTION - ESLINT_ERROR --> Parsing error: Unexpected token => : Source

I'm trying to implement like below:

Use of forEach

arr.forEach((num, index) => {
    return arr[index] = num * 2;
});

Use of Array.map():

let arr = [1, 2, 3, 4, 5];
let arr2 = arr.map(num => num * 2); 
consle.log('>> '+arr2);

Here is what I'm originally trying to implement:

let cleanedMID = mcWrapper.map(mId.newMID => this.cleanMerchantID(component, event, mId.newMID)); 
console.log('>>'+cleanedMID);

Is this(Array.map()) possible in lightning framework to implement.

  • 2
    Yes, both functions forEach() and map() are useable within Lightning Framework. You`ve got a typo somewhere. Please review your code. – Oleh Berehovskyi Apr 23 at 9:40
  • @OlehBerehovskyi Thanks for confirming that. Could you please review once the below code if it has some Typos. let cleanedMID = mcWrapper.map(mId.newMID => this.cleanMerchantID(component, event, mId.newMID)); console.log('>>'+cleanedMID); – SFDC Mafia - VIC Apr 23 at 9:47
1

The parameter you're passing to the arrow function should be the object from the array, not a specific item from the element. Notably, mId.newMID would be invalid, as a parameter name must follow JavaScript's rules for identifiers. Changing it to just mId should do the trick:

let cleanedMID = mcWrapper.map(
  mId => this.cleanMerchantID(component, event, mId.newMID)); 

Additional alterations may be required. Map expects the return value (here, whatever cleanMerchantID is) to be the new elements of the array. Without seeing that function, it's not entirely clear if this answer solves your problem, but it should point you in the right direction.

  • Thank you @sfdcfox, your answer just confirmed what I found out little later after posting this question. You have an answer for everything :) – SFDC Mafia - VIC Apr 23 at 10:27

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.