Skip to content

Instantly share code, notes, and snippets.

@tinkerer-red
Created October 30, 2024 20:20
Show Gist options
  • Select an option

  • Save tinkerer-red/11ac84d5fb72b736ce87bcdff9d5cfbc to your computer and use it in GitHub Desktop.

Select an option

Save tinkerer-red/11ac84d5fb72b736ce87bcdff9d5cfbc to your computer and use it in GitHub Desktop.
`serialize` removing functions from an output object
function serialize(_obj) {
if (is_handle(_obj) && script_exists(_obj)) return undefined; // strip functions as `undefined`
if (!is_array(_obj) && !is_struct(_obj)) return _obj;
// Check if it's an array or a plain object, then create a new instance accordingly
if (is_struct(_obj)) {
var _result = {};
var _names = struct_get_names(_obj)
var _length = array_length(_names);
for (var _i=0; _i<_length; _i++) {
_result[$ _names[_i]] = serialize(_obj[$ _names[_i]]);
}
}
else if (is_array(_obj)) {
var _result = [];
var _length = array_length(_obj);
for (var _i=0; _i<_length; _i++) {
_result[_i] = serialize(_obj[_i]);
}
}
return _result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment