/    Sign up×
Community /Pin to ProfileBookmark

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’
]
}
]
}
]
}
]
};[/CODE]

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;
}[/CODE]

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?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@rootJul 30.2014 — 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)
)
)
)
)
)
[/CODE]


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.
Copy linkTweet thisAlerts:
@Ronni_designauthorJul 30.2014 — 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.
Copy linkTweet thisAlerts:
@rootJul 30.2014 — 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.
Copy linkTweet thisAlerts:
@Ronni_designauthorJul 31.2014 — 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.
Copy linkTweet thisAlerts:
@tech_soul8Jul 31.2014 — See if something like this does what you need:

<i>
</i> 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]);
}
}
}
}
}
Copy linkTweet thisAlerts:
@rootJul 31.2014 — So its homework then...
×

Success!

Help @Ronni_design spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...