Skip to content

Instantly share code, notes, and snippets.

@cfsghost
Last active September 20, 2024 09:43
Show Gist options
  • Save cfsghost/1a33f71386589398787d2e5c8fd82580 to your computer and use it in GitHub Desktop.
Save cfsghost/1a33f71386589398787d2e5c8fd82580 to your computer and use it in GitHub Desktop.
Column updates
function mergeUpdates(data) {
let output = {};
Object.entries(data).forEach(function(entry) {
let key = entry[0];
let value = entry[1];
if (key.startsWith(fieldName + '.')) {
let column = key.split('.')[1];
output[column] = value;
return
}
output[key] = value;
})
return;
}
return mergeUpdates(source)
function getUpdates(fieldName) {
let updates = {};
Object.entries(source).forEach(function(entry) {
let key = entry[0];
let value = entry[1];
if (key.startsWith(fieldName + '.')) {
let column = key.split('.')[1];
updates[column] = value;
}
});
return updates;
}
return {
"_id": source._id,
"a": source.a,
"map_col": Object.assign({}, source.map_col, getUpdates('map_col'))
}
function getMapValue(fieldName) {
let updates = source[fieldName] || {};
Object.entries(source).forEach(function(entry) {
let key = entry[0];
let value = entry[1];
if (key.startsWith(fieldName + '.')) {
let column = key.split('.')[1];
updates[column] = value;
}
});
return updates;
}
return {
"_id": source._id,
"a": source.a,
"map_col": getMapValue('map_col')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment