2

Is there any way i can filter files with given extension and then further filter them for eg: I have .txt extension and i want to get all my .txt from an array

file= 
[ "animal_bio.txt",
  "xray.pdf",
  "fish_bio.txt",
  "mammal_doc.txt",
  "human_bio.txt",
  "machine.jpg"
]

filtered output contain all .txt extension and further it should contain all the files which have _bio.txt name in it.

so output look like

futherFile=
[ "human_bio.txt",
  "fish_bio.txt",
  "animal_bio.txt"
]
2
  • stackoverflow.com/questions/44199883/… Commented May 25, 2021 at 8:17
  • 2
    So you want all txt files and then group them by the suffix after _ - is that right? Did you try anything at all? Show your code Commented May 25, 2021 at 11:54

4 Answers 4

1

You can use String.protytype.endsWith function to compare the strings with your extension

const file = 
[ "animal_bio.txt",
  "xray.pdf",
  "fish_bio.txt",
  "mammal_doc.txt",
  "human_bio.txt",
  "machine.jpg"
]
result = file.filter((fileName) => fileName.endsWith("_bio.txt"));
console.log(result)

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the Array.filter method and use the String.endsWith method to filter. An example -

// List of files

file = ["animal_bio.txt",
  "xray.pdf",
  "fish_bio.txt",
  "mammal_doc.txt",
  "human_bio.txt",
  "machine.jpg"
]

// Filtering by extension

file.filter(x => x.endsWith(".txt"));

Hope it helped :)

Comments

1

You can easily achieve this result using reduce and match

When matching for the doc or bio, You can even restrict more to get the string only if _doc.txt is at end of the string using Regular expression /_bio.txt$/

const arr = [
  {
    id: "1",
    name: "animal_bio.txt",
  },
  {
    id: "2",
    name: "xray.pdf",
  },
  {
    id: "3",
    name: "animal_doc.txt",
  },
  {
    id: "4",
    name: "fish_doc.txt",
  },
  {
    id: "5",
    name: "flower_petals.jpg",
  },
  {
    id: "5",
    name: "plant_roots.jpg",
  },
  {
    id: "6",
    name: "human_image.jpg",
  },
  {
    id: "7",
    name: "human_bio.txt",
  },
  {
    id: "8",
    name: "mammal_doc.txt",
  },
];

const result = arr.reduce((acc, { name }) => {
    if (name.match(/\.txt$/)) {
      if (name.match(/_bio/)) {
        acc[0].push(name);
      } else {
        acc[1].push(name);
      }
    }
    return acc;
  },
  [[], []]
);

console.log(result);

Then you can get the element containing doc and bio using array destructuring as

const [bioArr, docArr] = result;
console.log(bioArr);
console.log(docArr);

const arr = [
  {
    id: "1",
    name: "animal_bio.txt",
  },
  {
    id: "2",
    name: "xray.pdf",
  },
  {
    id: "3",
    name: "animal_doc.txt",
  },
  {
    id: "4",
    name: "fish_doc.txt",
  },
  {
    id: "5",
    name: "flower_petals.jpg",
  },
  {
    id: "5",
    name: "plant_roots.jpg",
  },
  {
    id: "6",
    name: "human_image.jpg",
  },
  {
    id: "7",
    name: "human_bio.txt",
  },
  {
    id: "8",
    name: "mammal_doc.txt",
  },
];

const result = arr.reduce(
  (acc, { name }) => {
    if (name.match(/\.txt$/)) {
      if (name.match(/_bio/)) {
        acc[0].push(name);
      } else {
        acc[1].push(name);
      }
    }
    return acc;
  },
  [[], []]
);

const [bioArr, docArr] = result;
console.log(bioArr);
console.log(docArr);

1 Comment

Sorry, I wasn't clear enough :(. Using /\.txt/ to match an extension is a bug - I know because I made the same mistake once and it caused problems. Match /\.txt$/ to only match .txt as an extension but not if it occurs somewhere else in the filename.
0

you can use filter function from ES6 like:

const txtFile = file.filter((item) => (item.split('_'))[1] === 'bio.txt')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.