const data = {
name1: {
blblaDate: '12-31-2021',
arr: [
{
blablaDate: '12-31-2021',
},
],
},
name2: {
blblaDate: '12-31-2021',
arr: [
{
blablaDate: '12-31-2021',
},
],
},
}
Date
и изменить значения:blablaDate: '12-31-2021'
-> blablaDate: '31.12.2021'
const replaceValues = (val, test, replacer) =>
val instanceof Array
? val.map(n => replaceValues(n, test, replacer))
: val instanceof Object
? Object.fromEntries(Object
.entries(val)
.map(([ k, v ]) => [
k,
test(k, v)
? replacer(v)
: replaceValues(v, test, replacer)
])
)
: val;
const newData = replaceValues(
data,
k => k.includes('Date'),
v => v.replace(/(\d+)-(\d+)-/, '$2.$1.')
);
function recursive(data) {
for(const key in data) {
if(typeof data[key] === 'object') {
recursive(data[key]);
} else {
if(key.includes('Date')) {
data[key] = moment(data[key], 'MM-DD-YYYY').format('DD.MM.YYYY')
}
}
}
return data;
}