unfold
unfold function converts a key-value pair object into JSON object by unfolding it.
unfold(object, option?)Arguments
-
objecttype:
objectThe key-value pair object to unfold.
e.g.
{ "a.b": "c" }will be unfolded to{ a: { b: "c" } }. -
optiontype:
object-
arrayIndextype:
"dot" | "bracket"default:
"bracket"The index of array to unfold.
e.g.
{ "a[0]": "b" }will be unfolded to{ a: ["b"] }ifarrayIndexis"bracket", and will be unfolded to{ a: { 0: "b" } }ifarrayIndexis"dot". -
pruneArraytype:
booleandefault:
trueWhether to prune the array when unfolding.
e.g.
{ "a[1]": "b" }will be unfolded to{ a: [undefined, "b"] }ifpruneArrayisfalse, and will be unfolded to{ a: ["b"] }ifpruneArrayistrue.
-
Example
import { unfold } from 'json-origami';
const folded = { id: 1, name: 'John', age: 20, 'address.street': '123 Main St', 'address.city': 'New York', 'address.state': 'NY', 'address.zip': '10001',};
const unfolded = unfold(folded);/** * { * id: 1, * name: 'John', * age: 20, * address: { * street: '123 Main St', * city: 'New York', * state: 'NY', * zip: '10001', * }, * } */