javascript - Return all properties in JS object to "empty" state -



javascript - Return all properties in JS object to "empty" state -

i wonder if there's utility function in framework jquery truncate js object (there more 1 possible result, not sure useful yet):

{ // original object foo: "bar", arr: [1, 2, 3], sub: {a: 4, b: 5, subsub: {c: 6}} } { // truncated, variant 1 foo: "", arr: [], sub: {} } { // truncated, variant 2 foo: "", arr: [], sub: {subsub: {}} } { // truncated, variant 3 foo: "", arr: [], sub: {a: 0, b: 0, subsub: {c: 0}} }

if no, there solution more clever recursively iterate on properties, check types , remove / reset if necessary?

call constructor each property create empty object of same type.

for (var k in obj) { obj[k] = obj[k] && obj[k].constructor(); }

this homecoming numbers 0, booleans false, dates now, , regexps empty (but nans remain nan).

to recursively, retaining object-valued properties emptying them out (variant 3):

function truncate(obj) { (var k in obj) { var ctor = obj[k] && obj[k].constructor; if (ctor === object) truncate(obj[k]); else if (ctor) obj[k] = ctor(); } }

to eliminate numbers (variant 2), add together check:

function truncate(obj) { (var k in obj) { var ctor = obj[k] && obj[k].constructor; if (ctor === object) truncate(obj[k]); else if (ctor === number) delete obj[k]; else if (ctor) obj[k] = ctor(); } }

javascript object clear truncate

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -