-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
31 lines (28 loc) · 896 Bytes
/
Copy pathscript.js
File metadata and controls
31 lines (28 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Write a function to remove the hyphens '-' from all the properties (even nested) of the object
*/
const removeHyphens = (obj) => {
const newObj = {};
const recurse = (obj) => {
for (const key in obj) {
let value = obj[key];
if (value && typeof value === 'object') {
recurse(value, key);
} else {
let newValue = value.replace(/-/g, "");
newObj[key] = newValue;
}
}
}
recurse(obj);
return newObj;
}
// This object is just an example. The function should accept any kind of input.
const obj = {
orderData: {
name: "the-id",
items: [{ address: "primrose-street-london" }],
},
additional: "additional-prop",
};
console.log(JSON.stringify(removeHyphens(obj), null, 2));