0

I need help in javascript. I need to remove the object which already in the path.

As you can see there are 3 paths property.

1) golang 2) Root/DCL/JAVA 3) Root/DCL/JAVA/JAVA1/JAVA2

point 2 JAVA is the root of JAVA 2 folder. I need to remove the whole object from the array.

the object can be in any position.

[
  {
    level: '0',
    paths: 'golang',
    name: 'golang',
    updatedOn: 1998902546,
    type: 'folder',
    uuid: 'cd315c90-a9f8-48d9-9aed-a97b246b27e9',
    createdOn: 1998902546,
    elementType: 'folder'
  },
  {
    level: '4', //. remove this object in an array because Root/BCL/JAVA/JAVA1/JAVA2 (JAVA2)is a child of JAVA -> Root/BCL/JAVA
    paths: 'Root/BCL/JAVA/JAVA1/JAVA2',
    name: 'JAVA2',
    type: 'folder',
    elementType: 'folder',
    uuid: 'fe32e4b8-37be-4416-b129-852da83f5549',
    createdOn: 2113950571
  },
  { 
    level: '2', 
    paths: 'Root/BCL/JAVA',
    name: 'JAVA',
    updatedOn: 2039112906,
    type: 'folder',
    elementType: 'folder',
    uuid: 'cd315c90-a9f8-48d9-9aed-a97b246b27e7',
    creadedOn: 2039112906
  }
]
// split the paths by "/" and pushed in another array to match
for (let i = 0; i < list.length; i += 1) {
        const l = parseInt(list[i].level, 10)
        let splitArr = [];
        if (list[i].paths.length > 0) {
          splitArr = list[i].paths.split('/');
          arr2.push(splitArr)
        } else {
          arr2.push(list[i].paths)
        }

      }

not sure split is required. a bit lost now, please guide.

3
  • 1
    I don't understand what you want. Commented Jun 6, 2020 at 10:58
  • so you just want to strip the last node right? path.replace(/\/[^/]+$/,'') Commented Jun 6, 2020 at 11:03
  • added more description. hope you will get the context Commented Jun 6, 2020 at 11:08

1 Answer 1

1

filters on if it can find the string at position 0 of any other path.
You didn't specify what happens if paths are the same, but this will only keep the first one.

data = [
 {
    level: '0',
    paths: 'golang',
    name: 'golang',
    updatedOn: 1998902546,
    type: 'folder',
    uuid: 'cd315c90-a9f8-48d9-9aed-a97b246b27e9',
    createdOn: 1998902546,
    elementType: 'folder'
  },
  {
    level: '2',
    paths: 'Root/BCL/JAVA',
    name: 'JAVA',
    updatedOn: 2039112906,
    type: 'folder',
    elementType: 'folder',
    uuid: 'cd315c90-a9f8-48d9-9aed-a97b246b27e7',
    createdOn: 2039112906
  },
  {
    level: '4',
    paths: 'Root/BCL/JAVA/JAVA1/JAVA2',
    name: 'JAVA2',
    type: 'folder',
    elementType: 'folder',
    uuid: 'fe32e4b8-37be-4416-b129-852da83f5549',
    createdOn: 2113950571
  },
  {
    level: '2',
    paths: 'Root/BCL/JAVA',
    name: 'JAVA',
    updatedOn: 2039112906,
    type: 'folder',
    elementType: 'folder',
    uuid: 'cd315c90-a9f8-48d9-9aed-a97b246b27e7',
    createdOn: 2039112906
  },
  {
    level: '4',
    paths: 'Root/BCL/JAVA/JAVA1/JAVA2',
    name: 'JAVA2',
    type: 'folder',
    elementType: 'folder',
    uuid: 'x',
    createdOn: 2113950571
  },
  {
    level: '2',
    paths: 'Root/BCL/JAVA',
    name: 'JAVA',
    updatedOn: 2039112906,
    type: 'folder',
    elementType: 'folder',
    uuid: 'cd315c90-a9f8-48d9-9aed-a97b246b27e7',
    createdOn: 2039112906
  }
]
console.log(
data.filter((p,i)=>data.every((x,j)=>x===p||!p.paths.startsWith(x.paths)||(x.paths===p.paths&&i>j)))
)

Sign up to request clarification or add additional context in comments.

7 Comments

each user will have there own file structure. they can share folder/file with a group/user. I am getting all the folder which group can access. user can share the root dir and sub dir. i just wanted to find the root of the folder. if it's shared then remove the subfolder. I am wanted to achieve this via neo4j query. but don't know how to write it. that's trying this way
oh, so there won't be dupes I guess? Does this behave the way you want?
I think so. will try to modify the graph and test it out again. thanks, mate really appreciate
sorry it was other way around .. need to remove object Root/BCL/JAVA/JAVA1/JAVA2
Just have to swap the startsWith around.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.