Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
Search for substring in an object using full string

There are full paths to files like

libs/shared/util/something/tsconfig.spec.json
apps/project/backend/subfolder/project.json
this/has/no/match.json
root-file.json

and I need to check if the file is inside of a path in the given projects object:

const projects = {
    "just-a-library": "libs/shared/util/something",
    "project-backend": "apps/project/backend"
}

My problem is, that it will never fully match, but only the beginning of the string - as there are optional subfolders and of course filenames. If there is a match, it should return the object key.

So for libs/shared/util/something/tsconfig.spec.json it should return just-a-library and this/has/no/match.json there should be no result.

I can't use string.includes(substring) or string.indexOf(substring) as this is the other way round: The full string is the filepath input and I'm searching for a possible substring element.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If I get the task right...

const paths = [
  "libs/shared/util/something/tsconfig.spec.json",
  "apps/project/backend/subfolder/project.json",
  "this/has/no/match.json",
  "root-file.json",
];

const projects = {
  "just-a-library": "libs/shared/util/something",
  "project-backend": "apps/project/backend",
};

paths.forEach((path) => {
  const result = Object.keys(projects).find((k) => path.startsWith(projects[k])) || "[Not found]";
  console.log(`${path} - ${result}`);
});

Output:

libs/shared/util/something/tsconfig.spec.json - just-a-library
apps/project/backend/subfolder/project.json - project-backend
root-file.json - [Not found]
this/has/no/match.json - [Not found]
about 4 years ago · Juan Pablo Isaza Report

0

If you break input projects and file paths down, you can build a directory via an object. Once that's created scan through the object to find any matches.

Added some templating to show directory.

const projects = {
  "just-a-library": "libs/shared/util/something",
  "project-backend": "apps/project/backend"
};

const inputs = [
  "libs/shared/util/something/tsconfig.spec.json",
  "apps/project/backend/subfolder/project.json",
  "this/has/no/match.json",
  "root-file.json"
];

const createFolders = input => input.split(/\//g);
const directory = {};
const project = "__PROJECT__";
const directoryView = document.getElementById("directory");

const projectFolders = {};
Object.keys(projects).forEach(key => {
  projectFolders[key] = createFolders(projects[key]);
});

Object.keys(projectFolders).forEach(key => {
  let pwd = directory;
  for (let i = 0; i < projectFolders[key].length; i++) {
    if (!pwd[projectFolders[key][i]]) {
      pwd[projectFolders[key][i]] = {};
    }
    pwd = pwd[projectFolders[key][i]];
  }
  pwd[project] = key;
});

directoryView.innerText = JSON.stringify(directory, null, 2);

const inputFolders = inputs.map(entry => createFolders(entry));

for (let i = 0; i < inputFolders.length; i++) {
  let pwd = directory;
  const path = [];
  for (let j = 0; j < inputFolders[i].length; j++) {
    if (!pwd[inputFolders[i][j]] && j !== inputFolders[i].length - 1) {
      if (pwd === directory) {
        console.log("invalid input path:", inputFolders[i].join("/"));
      } else {
        console.log("file:", inputFolders[i].join("/"), "\nmatching project:", pwd[project]);
      }
      break;
    } else {
      pwd = pwd[inputFolders[i][j]];
      path.push(inputFolders[i][j]);
    }
  }
}
<pre id="directory" style="width:100px"></pre>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!