我要在其中添加一些后缀的数组值:

var arr = ["folder1/1.jpg","folder2/2.png","foldern/some.jpg"];


添加前缀“ _thumb”应导致以下结果:

newArr = ["folder1/1_thumb.jpg","folder2/2_thumb.png","foldern/some_thumb.jpg"];


这是我的代码,但不好:

var arr = ["folder1/1.jpg", "folder2/2.png", "foldern/some.jpg"];
  var newArr = [];
  arr.forEach(image => {
    var onlyPath = path.dirname(image);
    var filename = path.basename(image).split('.');
    newArr.push(onlyPath + '/' + filename[0] + '_thumb' + '.' + filename[1]);
  });
  console.log(newArr);

最佳答案

你可以使用Array.map



let arr = ["folder1/1.jpg","folder2/2.png","foldern/some.jpg"];
let newArr = arr.map(img => img.split('.').join('_thumb.'));
console.log(newArr)

关于javascript - 添加字符串文件的字符串后缀,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52814535/

10-16 03:11