Last active
August 25, 2017 10:28
-
-
Save daviddias/f8997fb0bdf4dc1c3543 to your computer and use it in GitHub Desktop.
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
function renameKeys (dict, keyMap) { | |
return _.reduce(dict, function (newDict, val, oldKey) { | |
var newKey | |
if (keyMap[oldKey]) { | |
newKey = keyMap[oldKey] | |
} else { | |
newKey = oldKey | |
} | |
newDict[newKey] = val | |
return newDict | |
}, {}) | |
} |
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
function renameKeys (dict, keyMap) { | |
return _.reduce(dict, remap, {}) | |
function remap (newDict, val, oldKey) { | |
var newKey | |
if (keyMap[oldKey]) { | |
newKey = keyMap[oldKey] | |
} else { | |
newKey = oldKey | |
} | |
if (val instanceof Object) { | |
newDict[newKey] = _.reduce(val, remap, {}) | |
} else { | |
newDict[newKey] = val | |
} | |
return newDict | |
} | |
} |
davidlondono
commented
Feb 1, 2017
•
This converts nested array to an Object. Here is the codepen link for your reference:
https://codepen.io/cksachdev/pen/NgZMYQ
Object structure
var obj = {
children: [{
id: "children id",
label: "children label",
type: 4,
uniqueId: "children unique id"
}],
id: "something",
label: "Some label here...",
type: 5,
uniqueId: "This is unique."
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment