使用节点 js / javascript 切片数组元素

slicing array element using node js / javascript

let array1= [
"home/work/data.jpg",
"home/work/abc.jpg",
"home/work/doc/animal.pdf",
"home/work/doc/fish_pdf.pdf"
];

array1= array1.map((data)=>{
return data.slice(2,data.length).join("/");
});
console.log(array1);

我想用“home/work/”分割我的数组,所以它看起来像这样

["data.jpg",
 "abc.jpg",
 "doc/animal.jpg",
 "doc/fish_pdf.pdf"]

但它不起作用 当我只使用这条线时

data.slice(2);

然后输出看起来像这样

let array1= [
"me/work/data.jpg",
"me/work/abc.jpg",
"me/work/doc/animal.pdf",
"me/work/doc/fish_pdf.pdf"
];

假设您想用静态字符串替换'home/work/'

let array1= [
"home/work/data.jpg",
"home/work/abc.jpg",
"home/work/doc/animal.pdf",
"home/work/doc/fish_pdf.pdf"
];

let array2= array1.map((data)=>{
return data.replace('home/work/','');
});
console.log(array2);

let array1= [
"home/work/data.jpg",
"home/work/abc.jpg",
"home/work/doc/animal.pdf",
"home/work/doc/fish_pdf.pdf"
];

array1= array1.map((data)=>{
return data.split('home/work/',data.length)[1];
});
console.log(array1);

你必须 split before using slice.

let array1 = [
  "home/work/data.jpg",
  "home/work/abc.jpg",
  "home/work/doc/animal.pdf",
  "home/work/doc/fish_pdf.pdf",
];

array1 = array1.map((data) => {
  return data.split("/").slice(2, data.length).join("/");
});
console.log(array1);