[Vuejs]-JavaScript – Modularize for-loop of a method

1👍

You can simply move your hard coding of the object from makeSummary function, instead pass it as parameter. which will give you flexibility of defining helper function as per your need.

function makeSummary(people, helper){
    const myList = []
    for (int i = 0; i < people.length; i++){
          myList.push(helper(people[i])
    }
    return myList
}


function helper1(person){
  return {
      fullName: person.firstName + " " + person.lastName,
      title: person.title,
  }
}

function helper2(person){
  return {
      fullName: person.firstName + " " + person.lastName,
      description: person.description,
  }
}

//call it like this
makeSummary(people, helper1)
makeSummary(people, helper2)

Leave a comment