You can loop throught these properties using Object.keys()
.
And for each property
check if its value isn't an object
and contains the date
string using k.match(/date/i)
so change it to a date
value. Otherwise if it's an object do the same with its properties in a recursive way.
This is what you need:
function transformProperties(obj) {
Object.keys(obj).forEach(function(k) {
if ((typeof obj[k]) !== "object" && k.match(/date/i)) {
obj[k] = new Date(obj[k]);
} else if ((typeof obj[k]) == "object") {
let sub = obj[k];
transformProperties(sub);
}
});
}
Demo:
var o = {
startDate: 2141242141,
adress: '',
endDate: 2141242141,
billings: {
startBillingDate: 2142421421421,
endBillingDate: 2142421421421
},
amount: 100
};
function transformProperties(obj) {
Object.keys(obj).forEach(function(k) {
if ((typeof obj[k]) !== "object" && k.match(/date/i)) {
obj[k] = new Date(obj[k]);
} else if ((typeof obj[k]) == "object") {
let sub = obj[k];
transformProperties(sub);
}
});
}
transformProperties(o);
console.log(o);