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 ?
array.filter().map(), but you can't combine them in one function, as they do different things.filterreturns a new array of different length, andmaptransforms every item in an array.mapdoesn't work because you are not returning anything form the callback.