Skip to content

Instantly share code, notes, and snippets.

@madeinfree
Last active September 20, 2019 03:36
Show Gist options
  • Save madeinfree/c634c947c16067707a7a617f7ed5df90 to your computer and use it in GitHub Desktop.
Save madeinfree/c634c947c16067707a7a617f7ed5df90 to your computer and use it in GitHub Desktop.
module.exports = {
generateMultipleInsertValues(data) {
return data.reduce((result, nextData, index) => {
result = result + Object.keys(nextData).reduce((values, key, keyIndex, allKeys) => {
if (keyIndex === 0) {
if (typeof nextData[key] === 'string') {
return values + `('${nextData[key]}', `
} else {
return values + `(${nextData[key]}, `
}
}
if (typeof nextData[key] === 'string') {
values = values + `'${nextData[key]}'`
} else {
values = values + `${nextData[key]}`
}
if (keyIndex !== allKeys.length - 1) {
values = values + `, `
}
return values
}, '')
if (index === data.length - 1) {
return `${result})`
} else {
return `${result}), `
}
}, '')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment