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

143
Views
Remove item from array according to condition

I have an array (given below). I want to remove files, if minified version of the same file exist.

var files = ["foo.js", "foo.min.js", "bar.css","bar.min.css"];

// Should return: [foo.min.js", "bar.min.css"]

var files = ["foo.js", "foo.min.js", "bar.css"];

// Should return: [foo.min.js", "bar.css"]

What i have tried

var files = ["foo.js", "bar.css"];

var tempUnMinified = [];
var tempMinified = [];
files.forEach(function (file) {
  if (file.endsWith(".min.js") || file.endsWith(".min.css")) {
    tempMinified.push(file);
  } else {
    tempUnMinified.push(file);
  }
});

tempUnMinified.forEach(function (unMinFile) {
  var tempRf = unMinFile.replace(".js", ".min.js").replace(".css", ".min.css");
  if (files.includes(tempRf)) {
    files.splice(files.indexOf(unMinFile), 1);
  }
});

console.log(files);

What i have tried does works, but i'm not sure if it works every time

and so on.. Thanks in advance

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

0

It can be done a little more concisely and in a way that's independent of file type...

const files = ["foo.js", "foo.min.js", "bar.css","bar.min.css"];
const remove = {}

files.forEach(fn => {
  let comps = fn.split('.');
  // is the middle token min?
  if (comps.length === 3 && comps[1] === 'min') {
    // remember to remove the file without the middle token
    remove[`${comps[0]}.${comps[2]}`] = true;
  }
});
console.log(`will remove: ${Object.keys(remove)}`);

const filtered = files.filter(fn => !remove[fn]);
console.log(filtered);

about 4 years ago · Juan Pablo Isaza Report

0

Use the map to manipulate the array data to remove ".min" and Use a set for the uniqueness of filenames

    const files = ["foo.js", "foo.min.js", "bar.css","bar.min.css"];
    const result = [...new Set(files.map(file => file.replace('.min', '')))];
    console.log(result)
about 4 years ago · Juan Pablo Isaza Report

0

You just need to create a custom implementation of the filter

const files = ["foo.js", "foo2.js", "foo2.css", "foo.min.js", "bar.css","bar.min.css"];


const result = files.filter((file) => {
  const miniPart = '.min.';
  if (file.includes(miniPart)) return true;

  const [name, extension] = file.split('.');
  const miniFile = `${name}${miniPart}${extension}`;

  return !files.includes(miniFile);
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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!