Skip to content

Instantly share code, notes, and snippets.

@6ewis
Last active June 3, 2016 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 6ewis/30339f06c20544fa3132b256aa8830dc to your computer and use it in GitHub Desktop.
Save 6ewis/30339f06c20544fa3132b256aa8830dc to your computer and use it in GitHub Desktop.
updateFormData(object) {
//initial state of address container
//addressContainer: {
// #1RegisteredAddress: {
// line_2: '2',
// line_3, '3'
// }
// #2DividendAddres: {
// Line2: "2",
// Line3: "3"
// }
//}
//we expect an object with a single key and value
const firstKey = (anyObject) => R.keys(anyObject)[0];
const rootObjectKey = firstKey(object); //#1RegisteredAddress
const rootObjectValue = object[rootObjectKey]; //{Line_1: 1}
//we also expect the nestedObject to have a single key and value
const nestedObjectKey = firstKey(rootObjectValue); //Line_1
const nestedObjectValue = R.prop(nestedObjectKey, rootObjectValue); //1
const updateNestedProp =
(acc, next) => R.assocPath([rootObjectKey, nestedObjectKey], nestedObjectValue, next);
const updatedState =
R.reduce(updateNestedProp, {}, [this.state.addressContainer]);
//final state of address container
//addressContainer: {
// #1RegisteredAddress: {
// Line_1: "1",
// line_2: '2',
// line_3, '3'
// }
// #2DividendAddres: {
// Line2: "2",
// Line3: "3"
// }
//}
this.setState({addressContainer: updatedState});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment