Is there an easy way to transform (rename) multipl...
# support
b
Is there an easy way to transform (rename) multiple properties at once?
m
Hey There! 👋 Your message has been received by the RudderStack team. We will forward this request to your Technical Account Manager, and they will get back to you shortly. Please use the thread for any additional comments.
b
We would like to append a string to all properties in our event calls. Is there a way to do it (e.g. with the
ForEach
method)? Thanks 🙏
h
You can easily accomplish that sure
b
Thanks, I found a solution:
Copy code
if (event && event.context && event.properties) {
        const keys = Object.keys(event.properties); // Rename all properties: Replace underscores with spaces, Capitalize
        if (keys) {
            keys.forEach(key => {
                if (event.properties[key]) {

                        event.properties[key.replace(/_/g, " ").replace(/\b./g, function(m){ return m.toUpperCase()})] = event.properties[key];
                        delete event.properties[key]
                        
                    } 
            })
        }
    }
h
you can just do
Copy code
Object.keys(event.properties).forEach(key => {
b
Nice! Follow up: Is it possible to exclude properties based on an allowlist?
h
Something like
Copy code
let excludePropsList=['p1','p2','p3'];

let eventProps = {'p1': 1,'p2': 2,'p3': 3,'p4': 4,'p5': 5};

Object.keys(eventProps).forEach(key => {
 if (excludePropsList.indexOf(key) === -1){
   eventProps[key+'_newprop'] = eventProps[key] ;
   delete eventProps[key];
 }
});
gives you
m
Thanks man @hundreds-lifeguard-65770
b
works well, thanks 🙏
m
Redsquare is the expert :)
😊 1