Created
December 8, 2020 00:51
-
-
Save OzanKurt/1df41a9380ffa6d86acaf7105d07119f to your computer and use it in GitHub Desktop.
Laravel's Arr::dot function for Javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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