Created
May 13, 2010 18:40
-
-
Save alihasan/400220 to your computer and use it in GitHub Desktop.
Flattens part of an object into the parent object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Name: jquery.flattenObjectPoint.js | |
Description: Flattens part of an object into the parent object. | |
Usage: | |
var foo = { | |
a : ‘hello’, | |
b: ‘world’ | |
c: { | |
a : ‘hello’, | |
b: ‘world’ | |
}, | |
d: ‘goodbye’ | |
}; | |
$.flattenObjectPoint(foo,’c’,true); | |
Output: | |
var foo = { | |
a : ‘hello’, | |
b: ‘world’ | |
c_a : ‘hello’, | |
c_b: ‘world’ | |
d: ‘goodbye’ | |
}; | |
*/ | |
(function($){ | |
$.flattenObjectPoint : function(parent,nodeName,prefix) { | |
var node = parent[nodeName], leafName; | |
if (node !== undefined) { | |
$.each(node, function(i, leaf){ | |
leafName = (prefix !== undefined && prefix) ? nodeName + ‘_’ + i : i; | |
parent[leafName] = leaf; | |
}); | |
delete parent[nodeName]; | |
} | |
return parent; | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment