From: Sven Schöling Date: Fri, 19 Nov 2021 23:51:16 +0000 (+0100) Subject: kivi.serialize - javascript flatten X-Git-Tag: kivitendo-mebil_0.1-0~10^2~2^2~216^2~53 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=8496b280c5142ead5a0c211f50865b32586d3b5c;p=kivitendo-erp.git kivi.serialize - javascript flatten --- diff --git a/js/kivi.js b/js/kivi.js index 7fb250149..31a56a443 100644 --- a/js/kivi.js +++ b/js/kivi.js @@ -699,6 +699,26 @@ namespace("kivi", function(ns) { return kivi._shell_escape(elt); }).join(' '); }; + + ns.serialize = function(source, target = [], prefix, in_array = false) { + let arr_prefix = first => in_array ? (first ? "[+]" : "[]") : ""; + + if (Array.isArray(source) ) { + source.forEach(( val, i ) => { + ns.serialize(val, target, prefix + arr_prefix(i == 0), true); + }); + } else if (typeof source === "object") { + let first = true; + for (let key in source) { + ns.serialize(source[key], target, (prefix !== undefined ? prefix + arr_prefix(first) + "." : "") + key); + first = false; + } + } else { + target.push({ name: prefix + arr_prefix(false), value: source }); + } + + return target; + }; }); kivi = namespace('kivi');