Skip to content

Instantly share code, notes, and snippets.

@alihasan
Created May 13, 2010 18:40
Show Gist options
  • Save alihasan/400220 to your computer and use it in GitHub Desktop.
Save alihasan/400220 to your computer and use it in GitHub Desktop.
Flattens part of an object into the parent object.
/*
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