# Compacting this repetitive JavaScript bit which trims values in an object

    shippingAddress.first_name = shippingAddress.first_name.trim()


What is a more elegant way to write this? I am thinking I can map it somehow or use something similar to Ruby's inject but I'm not sure.

With single traversal on keys list and trimming all fields of type string:
Object.keys(shippingAddress).forEach((k) => {

• @McDerp, with for loop you would have to declare additional counter variables and control condition checked on each iteration. You don't need that when dealing with object keys – RomanPerekhrest Dec 22 '19 at 5:54