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