Answer the question
In order to leave comments, you need to log in
How can the previous value of an object be added to the current value?
Hello. There is a type path: you need to get an array of objects from it:const path = 'dir1/dir2/dir3/dir4/';
const arr = [
{ key: 'dir1', path: 'dir1/' },
{ key: 'dir2', path: 'dir1/dir2/' },
{ key: 'dir3', path: 'dir1/dir2/dir3/' },
{ key: 'dir4', path: 'dir1/dir2/dir3/dir4/' },
]
Answer the question
In order to leave comments, you need to log in
const path = 'dir1/dir2/dir3/dir4/';
const result =
path
.split('/')
.slice(0,-1)
.map((key, i, self) => ({key, path: self.slice(0, i+1).join('/') + '/'});
const paths = path.split("/")
paths.forEach(function (item) {
if(item) arr.push(
{key: item, path: path.substring(0, path.indexOf(item)) + item})
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question