Skip to content

Instantly share code, notes, and snippets.

@OzanKurt
Created December 8, 2020 00:51
Show Gist options
  • Save OzanKurt/1df41a9380ffa6d86acaf7105d07119f to your computer and use it in GitHub Desktop.
Save OzanKurt/1df41a9380ffa6d86acaf7105d07119f to your computer and use it in GitHub Desktop.
Laravel's Arr::dot function for Javascript.
/**
* Laravel's Arr::dot function for Javascript.
* IMPORTANT: Requires lodash installed.
*/
function dot(array, prepend) {
results = []
prepend = prepend || ''
$.each(array, function(key, value) {
if ((_.isObject(value) || _.isArray(value)) && ! _.isEmpty(value)) {
results = {
...results,
...dot(value, prepend + key + '.')
};
} else {
results[prepend + key] = value;
}
});
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment