//function for creating a shallow object and remove the multiple spaces
export const getTrimmedValues = (values) => {
const shallowValues = {
...values,
};
for (const key in shallowValues.primaryInformation) {
const currentValue = shallowValues.primaryInformation[key];
if (typeof currentValue === 'string') {
shallowValues.primaryInformation[key] = currentValue.replace(/\s+/g, ' ').trim();
}
}
return shallowValues;
};
//Original Object
const values = {
otherObject: {}
otherArray: []
primaryInformation: {
email: "[email protected]"
externalId: "DSB-External test"
firstName: "Dave External test test"
isGood: true
isHeaven: false
lastName: "Bacay External"
userId: 656555
}
}
//calling the function
getTrimmedValues(values)
I want to create a shallow object from the original object and edit the string to remove the multiple spaces by using shallow object and for
loop, I think I implemented it in a wrong way.
All advices and comments are appreciated.
Here we can take advantage of JSON.stringify function has a second parameter as a replacer function is internally iterating on every key of object. Please check the below code.