Answer the question
In order to leave comments, you need to log in
How to change a value in an object along its path?
There is an array of objects in which there is a large nesting, including through arrays. For example:
export const data = [
{
created_at: "30.08.2021",
entry: {
created_at: "30.08.2021",
},
arr: [
{
id: 111,
created_at: "30.08.2021",
updated_at: "30.08.2021",
subEntry: {
id: 222,
created_at: "30.08.2021",
updated_at: null,
date_assigned_on: "09.09.2021",
date_prepared_for: "10.09.2021",
}
}
]
}
];
[
'created_at',
'entry.created_at',
'arr.0.created_at',
'arr.0.subEntry.updated_at',
]
Answer the question
In order to leave comments, you need to log in
function setNestedVal(root, path, val) {
const keys = path.split('.');
const key = keys.pop();
keys.reduce((acc, n) => acc[n] ??= {}, root)[key] = val;
}
setNestedVal(data, '0.arr.0.str', 'hello, world!!');
setNestedVal(data, '0.arr.1.num', 666);
Interested in turnkey solutions?
We need to modify this array by converting all strings with dates in it, no matter how deeply nested, into objects.
const replaceValues = (data, test, transform) =>
test(data)
? transform(data)
: data instanceof Array
? data.map(n => replaceValues(n, test, transform))
: data instanceof Object
? Object.fromEntries(Object
.entries(data)
.map(([ k, v ]) => [ k, replaceValues(v, test, transform) ])
)
: data;
const newData = replaceValues(
data,
x => typeof x === 'string' && /^\d{2}\.\d{2}\.\d{4}$/.test(x),
str => new Date(str.split('.').reverse().join('-'))
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question