snowberb – 15-27 Jul 6

can someone explain to me what this code does in like a really dumb way so everyone could understand it?
const parts = path
.slice(1)
.split("/")
.filter((i) => Boolean(i));
let res = obj;

for (let i = 0; i < parts.length; i++) {
let part = parts[i];
res = res[part];
}
return res
const parts = path
.slice(1)
.split("/")
.filter((i) => Boolean(i));
let res = obj;

for (let i = 0; i < parts.length; i++) {
let part = parts[i];
res = res[part];
}
return res
L
lebouwski287d ago
const parts = path
.slice(1)
.split("/")
.filter((i) => Boolean(i));
const parts = path
.slice(1)
.split("/")
.filter((i) => Boolean(i));
this takes a path like "/foo/bar/baz" and turns it into ['foo', 'bar', 'baz']
let res = obj;
let res = obj;
this is presumably a directory tree, an object like
{
foo: {
bar: {
baz: 42
}
}
}
{
foo: {
bar: {
baz: 42
}
}
}
and then
for (let i = 0; i < parts.length; i++) {
let part = parts[i];
res = res[part];
}
for (let i = 0; i < parts.length; i++) {
let part = parts[i];
res = res[part];
}
traverses the object, going in to each folder. so at the end you'd be left with 42
UU
Unknown User287d ago
L
lebouwski287d ago
yeah filter(boolean) handles that too
UU
Unknown User285d ago