Filter object with folder/file structure and return structure filtered by name

Asked

Viewed 19 times

0

Hello, I have the following data structure that I receive in Backend JSON format:

const tree =     {
      "treeState":{
         "opened":{
    
         },
         "selected":null
      },
      "path":"./sounds/",
      "name":"sounds",
      "children":[
         {
            "path":"sounds/Default",
            "name":"Default",
            "children":[
               {
                  "path":"sounds/Default/Evil-dark-rising-sound-effect.mp3",
                  "name":"Evil-dark-rising-sound-effect.mp3",
                  "size":638850,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/Default/labrador-barking-daniel_simon.mp3",
                  "name":"labrador-barking-daniel_simon.mp3",
                  "size":351117,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/Default/teste_XPP.mp3",
                  "name":"teste_XPP.mp3",
                  "size":13365217,
                  "extension":".mp3",
                  "type":"file"
               }
            ],
            "size":14355184,
            "type":"directory"
         },
         {
            "path":"sounds/Evil-dark-rising-sound-effect.mp3",
            "name":"Evil-dark-rising-sound-effect.mp3",
            "size":638850,
            "extension":".mp3",
            "type":"file"
         },
         {
            "path":"sounds/QQQQQQQ.msi",
            "name":"QQQQQQQ.msi",
            "size":109166592,
            "extension":".msi",
            "type":"file"
         },
         {
            "path":"sounds/Sounds",
            "name":"Sounds",
            "children":[
               {
                  "path":"sounds/Sounds/labrador-barking-daniel_simon.mp3",
                  "name":"labrador-barking-daniel_simon.mp3",
                  "size":351117,
                  "extension":".mp3",
                  "type":"file"
               }
            ],
            "size":351117,
            "type":"directory"
         },
         {
            "path":"sounds/labrador-barking-daniel_simon.mp3",
            "name":"labrador-barking-daniel_simon.mp3",
            "size":351117,
            "extension":".mp3",
            "type":"file"
         },
         {
            "path":"sounds/test1",
            "name":"test1",
            "children":[
               {
                  "path":"sounds/test1/0107.mp3",
                  "name":"0107.mp3",
                  "size":46281,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/test1/0110.mp3",
                  "name":"0110.mp3",
                  "size":42754,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/test1/Evil-dark-rising-sound-effect.mp3",
                  "name":"Evil-dark-rising-sound-effect.mp3",
                  "size":638850,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/test1/Guard-dog-barking-sound-effect (1).zip",
                  "name":"Guard-dog-barking-sound-effect (1).zip",
                  "size":298749,
                  "extension":".zip",
                  "type":"file"
               },
               {
                  "path":"sounds/test1/AAA.png",
                  "name":"AAA.png",
                  "size":13989,
                  "extension":".png",
                  "type":"file"
               },
               {
                  "path":"sounds/test1/labrador-barking-daniel_simon.mp3",
                  "name":"labrador-barking-daniel_simon.mp3",
                  "size":351117,
                  "extension":".mp3",
                  "type":"file"
               }
            ],
            "size":1391740,
            "type":"directory"
         },
         {
            "path":"sounds/test2",
            "name":"test2",
            "children":[
               {
                  "path":"sounds/test2/0107.mp3",
                  "name":"0107.mp3",
                  "size":46281,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/0110.mp3",
                  "name":"0110.mp3",
                  "size":42754,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/22625871_moody-piano_by_orangehead_preview.mp3",
                  "name":"22625871_moody-piano_by_orangehead_preview.mp3",
                  "size":5526350,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/OOO.pdf",
                  "name":"OOO.pdf",
                  "size":55617,
                  "extension":".pdf",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/III.docx",
                  "name":"III.docx",
                  "size":2736,
                  "extension":".docx",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/Evil-dark-rising-sound-effect.mp3",
                  "name":"Evil-dark-rising-sound-effect.mp3",
                  "size":638850,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/SSS.001.png",
                  "name":"SSS.001.png",
                  "size":35324,
                  "extension":".png",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/OOO.zip",
                  "name":"OOO.zip",
                  "size":2657906,
                  "extension":".zip",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/labrador-barking-daniel_simon.mp3",
                  "name":"labrador-barking-daniel_simon.mp3",
                  "size":351117,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/madona_show.mp3",
                  "name":"madona_show.mp3",
                  "size":13365217,
                  "extension":".mp3",
                  "type":"file"
               }
            ],
            "size":22722152,
            "type":"directory"
         }
      ],
      "size":148976752,
      "type":"directory",
      "BASE_URL":"https://example:5555/"
 }

const isFile = node => (node.type === 'file');
const isDirectory = node => (node.type === 'directory');
const isMatched = (value, searchValue) => {
  const trimTextFilter = searchValue && searchValue.replace(/  +/g, ' ');
  const regex = new RegExp(`.*${trimTextFilter}.*`, 'gmi');
  return !!value.match(regex);
}

const valorPesquisaTest = 'madona_show';

function filterName(node, index) {
  if(isDirectory(node)) {
    node.children = node.children.filter(filterName);
    const state = node.children.length ? true : false;
    if (!state) {
      node = null;
    }
    return node;
  }

  if(isFile(node)) {
    const state = isMatched(node.name, valorPesquisaTest);
    if(!state) {
      node = null;
    }
    return node;
  }
 }
 
const result = tree.children.map(filterName);
console.log(result);
      

I want to filter this data structure so that it returns the updated tree with the files that match. Can someone shed some light or a path I can follow? Thank you.

  • What result do you want to get ? In the snippet you have in the question you already see a result with the name mandoa_show of research

1 answer

0

const tree =     {
      "treeState":{
         "opened":{
    
         },
         "selected":null
      },
      "path":"./sounds/",
      "name":"sounds",
      "children":[
         {
            "path":"sounds/Default",
            "name":"Default",
            "children":[
               {
                  "path":"sounds/Default/Evil-dark-rising-sound-effect.mp3",
                  "name":"Evil-dark-rising-sound-effect.mp3",
                  "size":638850,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/Default/labrador-barking-daniel_simon.mp3",
                  "name":"labrador-barking-daniel_simon.mp3",
                  "size":351117,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/Default/teste_XPP.mp3",
                  "name":"teste_XPP.mp3",
                  "size":13365217,
                  "extension":".mp3",
                  "type":"file"
               }
            ],
            "size":14355184,
            "type":"directory"
         },
         {
            "path":"sounds/Evil-dark-rising-sound-effect.mp3",
            "name":"Evil-dark-rising-sound-effect.mp3",
            "size":638850,
            "extension":".mp3",
            "type":"file"
         },
         {
            "path":"sounds/QQQQQQQ.msi",
            "name":"QQQQQQQ.msi",
            "size":109166592,
            "extension":".msi",
            "type":"file"
         },
         {
            "path":"sounds/Sounds",
            "name":"Sounds",
            "children":[
               {
                  "path":"sounds/Sounds/labrador-barking-daniel_simon.mp3",
                  "name":"labrador-barking-daniel_simon.mp3",
                  "size":351117,
                  "extension":".mp3",
                  "type":"file"
               }
            ],
            "size":351117,
            "type":"directory"
         },
         {
            "path":"sounds/labrador-barking-daniel_simon.mp3",
            "name":"labrador-barking-daniel_simon.mp3",
            "size":351117,
            "extension":".mp3",
            "type":"file"
         },
         {
            "path":"sounds/test1",
            "name":"test1",
            "children":[
               {
                  "path":"sounds/test1/0107.mp3",
                  "name":"0107.mp3",
                  "size":46281,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/test1/0110.mp3",
                  "name":"0110.mp3",
                  "size":42754,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/test1/Evil-dark-rising-sound-effect.mp3",
                  "name":"Evil-dark-rising-sound-effect.mp3",
                  "size":638850,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/test1/Guard-dog-barking-sound-effect (1).zip",
                  "name":"Guard-dog-barking-sound-effect (1).zip",
                  "size":298749,
                  "extension":".zip",
                  "type":"file"
               },
               {
                  "path":"sounds/test1/AAA.png",
                  "name":"AAA.png",
                  "size":13989,
                  "extension":".png",
                  "type":"file"
               },
               {
                  "path":"sounds/test1/labrador-barking-daniel_simon.mp3",
                  "name":"labrador-barking-daniel_simon.mp3",
                  "size":351117,
                  "extension":".mp3",
                  "type":"file"
               }
            ],
            "size":1391740,
            "type":"directory"
         },
         {
            "path":"sounds/test2",
            "name":"test2",
            "children":[
               {
                  "path":"sounds/test2/0107.mp3",
                  "name":"0107.mp3",
                  "size":46281,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/0110.mp3",
                  "name":"0110.mp3",
                  "size":42754,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/22625871_moody-piano_by_orangehead_preview.mp3",
                  "name":"22625871_moody-piano_by_orangehead_preview.mp3",
                  "size":5526350,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/OOO.pdf",
                  "name":"OOO.pdf",
                  "size":55617,
                  "extension":".pdf",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/III.docx",
                  "name":"III.docx",
                  "size":2736,
                  "extension":".docx",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/Evil-dark-rising-sound-effect.mp3",
                  "name":"Evil-dark-rising-sound-effect.mp3",
                  "size":638850,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/SSS.001.png",
                  "name":"SSS.001.png",
                  "size":35324,
                  "extension":".png",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/OOO.zip",
                  "name":"OOO.zip",
                  "size":2657906,
                  "extension":".zip",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/labrador-barking-daniel_simon.mp3",
                  "name":"labrador-barking-daniel_simon.mp3",
                  "size":351117,
                  "extension":".mp3",
                  "type":"file"
               },
               {
                  "path":"sounds/test2/madona_show.mp3",
                  "name":"madona_show.mp3",
                  "size":13365217,
                  "extension":".mp3",
                  "type":"file"
               }
            ],
            "size":22722152,
            "type":"directory"
         }
      ],
      "size":148976752,
      "type":"directory",
      "BASE_URL":"https://example:5555/"
 }

const isFile = node => (node.type === 'file');
const isDirectory = node => (node.type === 'directory');
const isMatched = (value, searchValue) => {
  const trimTextFilter = searchValue && searchValue.replace(/  +/g, ' ');
  const regex = new RegExp(`.*${trimTextFilter}.*`, 'gmi');
  return !!value.match(regex);
}

const valorPesquisaTest = 'madona_show';

function filterName(node, index) {
  if(isDirectory(node)) {
    node.children = node.children.filter(filterName);
    const state = node.children.length ? true : false;
    if (!state) {
      return;
    }
    return node;
  }

  if(isFile(node)) {
    const state = isMatched(node.name, valorPesquisaTest);
    if(!state) {
     return;
    }
    return node;
  }
 }
 
const result = [tree].map(filterName);
console.log(result);

I already found the solution, just put everything in an array and made the map: The only change I made was: const result = [tree].map(filterName);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.