Skip to content

Instantly share code, notes, and snippets.

@robophil
Created July 24, 2017 00:27
Show Gist options
  • Save robophil/e17955a3d9c7a01adbc94d59ff3b6601 to your computer and use it in GitHub Desktop.
Save robophil/e17955a3d9c7a01adbc94d59ff3b6601 to your computer and use it in GitHub Desktop.
Simple approach to mapping contact data
const contacts = [{id: 1, name: "DF lade", country: "nig", no: 090}, {id: 2, name: "Miss Akesuwa", country: "nig", no: 090}, {id: 3, name: "wendy", country: "nig", no: 090}, ]
const message = `Hello {{name}}, welcome to {{country}}`
function validate(message){
return true
}
const contactKeys = Object.keys(contacts[0])
const newMessages = contacts.map(contact => {
let msg = message
contactKeys.forEach(key => {
msg = msg.replace(`{{${key}}}`, contact[key])
})
return {msg, number: contact.no}
})
console.log(newMessages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment