www.webdeveloper.com
Results 1 to 7 of 7

Thread: Extract value from array recursively in javascript

  1. #1
    Join Date
    Feb 2013
    Posts
    21

    Extract value from array recursively in javascript

    I want to extract the file names in this structure recursively. -
    Code:
    var DirectoryTree = {
        dir: 'project1',
        files: [
            'proj.config', {
                dir: 'src',
                files: [
                    'name.htm',
                    'dept.htm',
                    'salary.htm', {
                        dir: 'scripts',
                        files: [
                            'name.js',
                            'dept.js',
                            'salary.js'
                        ]
                    }
                ]
            }, {
                dir: 'project2',
                files: [
                    'proj.config', {
                        dir: 'src',
                        files: [
                            'name.htm',
                            'dept.htm',
                            'salary.htm', {
                                dir: 'scripts',
                                files: [
                                    'name.js',
                                    'dept.js',
                                    'salary.js'
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    };
    I wrote the following code and got all the file names. But now I also want to extract the file names that are under a subfolder i.e src. I pass the subfolder name as the second argument. Here is the code now -

    Code:
    fileNames : function(fileSystem, subFolder){
    		var result = [];
    		
    		(function findFiles(fileSystem){
    			var files = fileSystem.files;
    			
    			if(typeof files !== 'undefined'){
    				for(var i=0; i<files.length; i++){
    					if(typeof files[i] === 'string') {
    						result.push(files[i]);
    					}else{
    						findFiles(files[i]);
    					}
    				}
    			}
    		})(fileSystem);
    	
    		return result;
    	}
    The code now returns all the file names in the directory structure. I want to modify this to support both functionality. It should return all the file names in the structure and when there is a value passed into the subfolder it gives all the files beneath it. So in this case when i pass "SRC" as my subfolder it should return 5 files 'name.htm', 'dept.htm', 'salary.htm', 'name.htm', 'dept.htm', 'salary.htm'.

    How can i do this?

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,413
    To be honest, I really don't know what your objective is using such an intensive structure and what you are trying to achieve, Basically you have this

    Code:
    Object (
        String, 
            Array( 
                String,
                   Object(
                       String,
                           Array( String,String,String,
                               Object( String,
                                   Array(String,String,String) 
                               )
                           )
                   ) 
            )
    )
    IMHO you need to simplify the data structure or see an alternative like PHP to deliver a structure based on the users machine or a login.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Feb 2013
    Posts
    21
    Unfortunately, I cant change the folder structure. I think if i can get the name of the dir as "src" recursively, then i can call the findFiles function and get the five filenames. But the problem is that i am facing errors while doing this.

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,413
    What I meant was that your data structure is overly complex and you should seek a simpler solution to your problem, it matters not what the actual files location is as long as you either reference the path or can map it by use of a function to gather those details.

    As you have only shown the function and nothing about how the function is used, it is hard to say if this problem can be solved by a re-think of your problem. It would help if you threw in your HTML that relates to this function.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  5. #5
    Join Date
    Feb 2013
    Posts
    21
    This is a Javascript exercise on recursion that i am trying to solve. It has no HTML. I have some test cases and I have to write code accordingly in order to pass those test cases. The first test case was to return all the files that are present in the structure. I achieved that by the the code that I have posted. But when I have to return the filenames under a certain folder, I am running into issues.

  6. #6
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    See if something like this does what you need:

    Code:
     function listFiles(tree) {
             if (typeof tree === "object") {
                for (var i in tree) {
                   if (typeof tree[i] === "object") {
                      console.log(i + ":");
                      listFiles(tree[i]);
                   } else {
                      if (Object.prototype.toString.call(tree) === "[object Array]")
                         console.log("    " + tree[i]);
                      else {
                         console.log(i + ":");
                         console.log("    " + tree[i]);
                      }
                   }
                }
             }
          }

  7. #7
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,413
    So its homework then...
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles