javascript - Covert JS object into flat array with parent names -
i have object this:
data: { connection: { type: 0, connected: false }, acceleration: { x: 0, y: 0, z: 0, watchid: 0, haserror: false } },
converting flat array this:
"connected": false "haserror": false "type": 0 "watchid": 0 "x": 0 "y": 0 "z": 0
is easy task (recurrence friend!).
but there way in javascript called full parents, i.e. this:
"connection.connected": false "acceleration.haserror": false "connection.type": 0 "acceleration.watchid": 0 "acceleration.x": 0 "acceleration.y": 0 "acceleration.z": 0
or expecting much?
another variant:
function flatten(o) { var prefix = arguments[1] || "", out = arguments[2] || {}, name; (name in o) { if (o.hasownproperty(name)) { typeof o[name] === "object" ? flatten(o[name], prefix + name + '.', out) : out[prefix + name] = o[name]; } } return out; }
invoke flatten(data);
Comments
Post a Comment