R
Reactiflux

snowberb – 15-27 Jul 6

# snowberb – 15-27 Jul 6

S⛄Snowberb⛄7/6/2023
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``
Llebouwski7/6/2023
``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`
UUUnknown User7/6/2023
Message Not Public
Llebouwski7/6/2023
yeah filter(boolean) handles that too
UUUnknown User7/8/2023
Message Not Public