当我尝试使用注释代码(多行reduce语句)时,它可以正常工作。 然而当我尝试使用单行reduce语句时,它却不能正常工作。 我理解这是由于reduce语句中的splice命令造成的。 有人能否建议如何克服这种情况以编写单行reduce语句?
function order(words){
// ...
let regex = /\d+/g;
let matches = words.match(regex)
// return words.split(' ').reduce((finalar, element, indx) => {
// console.log('matches', matches);
// finalar.splice(matches[indx] - 1, 0, element)
// console.log('element', element);
// return finalar;
// }, []).join(' ');
return words.split(' ').reduce((finalar, element, indx) => finalar.splice([matches[indx] - 1],0,element) , []);
}
console.log(order("is2 Thi1s T4est 3a")); //Output: Thi1s is2 3a T4est
当我尝试使用注释代码(多行reduce语句)时,它可以正常工作。 然而,当我尝试使用单行reduce语句时,它不起作用。 我理解这是由于reduce语句中的splice命令造成的。 有人能否建议如何克服这种情况以编写单行reduce语句?