0

I have an array which look like this

["home/work/abc.jpg",
 "home/work/fish.pdf",
 "home/work/fish.jpg",
 "home/work/doc/animal.jpg",
 "home/work/doc/animal.pdf"];

so I want to filter array which contain ".jpg" extension file so I filtered it out by using

 array= array.filter((data)=>{
 return data.indexOf(".jpg")>=0
 });

so I got my expected value as

[ "home/work/abc.jpg",
  "home/work/fish.jpg",
  "home/work/doc/animal.jpg"
]

and I replace "home/work/" by using map function

 let rep="home/work/";
 array = array.map((data)=>{
 data.replace(rep,"")
 });

and got my value as

[ "abc.jpg",
  "fish.jpg",
  "doc/animal.jpg"
]

but the problem is I have to use two method to filter and replace them is there any possibility I can merge this two method and minimise my code

 array= array.filter((data)=>{
 return data.indexOf(".jpg")>=0
 });

 let rep="home/work/";
 array = array.map((data)=>{
 data.replace(rep,"")
 });

expected output

[ "abc.jpg",
  "fish.jpg",
  "doc/animal.jpg"
]

By using any chaining method ?

2
  • You can chain array.filter().map(), but you can't combine them in one function, as they do different things. filter returns a new array of different length, and map transforms every item in an array. Commented Jun 7, 2021 at 7:01
  • 1
    Also, the map doesn't work because you are not returning anything form the callback. Commented Jun 7, 2021 at 7:02

3 Answers 3

2

You can chain off of the filtered array without creating another variable, and by using implicit return to make things more concise:

const filenames = ["home/work/abc.jpg",
 "home/work/fish.pdf",
 "home/work/fish.jpg",
 "home/work/doc/animal.jpg",
 "home/work/doc/animal.pdf"];
const rep="home/work/";
const result = filenames
  .filter(file => file.includes('.jpg'))
  .map(file => file.replace(rep, ''));
console.log(result);

To actually do it in a single iteration, you'd have to give up on the chaining, and use reduce or a standard iteration method.

const filenames = ["home/work/abc.jpg",
 "home/work/fish.pdf",
 "home/work/fish.jpg",
 "home/work/doc/animal.jpg",
 "home/work/doc/animal.pdf"];
const rep="home/work/";
const result = [];
for (const file of filenames) {
  if (file.includes('.jpg')) result.push(file.replace(rep, ''));
}
console.log(result);

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

Comments

1

You can obtain same result using the same two method that you've used but inside reduce

const arr = [
  "home/work/abc.jpg",
  "home/work/fish.pdf",
  "home/work/fish.jpg",
  "home/work/doc/animal.jpg",
  "home/work/doc/animal.pdf",
];

const result = arr.reduce((acc, curr) => {
  if (curr.indexOf(".jpg") >= 0) {
    acc.push(curr.replace("home/work/", ""));
  }
  return acc;
}, []);

console.log(result);

Comments

1

Try this. This will return the same output you are looking for. Using both .map and .filter combined.

 let array = array
   .filter((data) => data.indexOf(".jpg")>=0)
   .map((data) => data.replace("home/work/",""))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.