Javascript 逻辑错误!使用字符串在指定位置插入数组

Javascript 逻辑错误!使用字符串在指定位置插入数组,javascript,node.js,arrays,sorting,Javascript,Node.js,Arrays,Sorting,我写了一个函数来提取包含在单词中的单词索引, 例如:4 Fo1r PE6人员的2这应该按顺序排列, 下面是代码 function order(words){ var result = [] var sorting = words.split(' ').sort() console.log(sorting); for(let i=0; i<sorting.length;i++) { var temp = sorting[i].split(

我写了一个函数来提取包含在单词中的单词索引, 例如:4 Fo1r PE6人员的2这应该按顺序排列, 下面是代码

function order(words){
    var result = []
    var sorting = words.split(' ').sort() 
    console.log(sorting);
    for(let i=0; i<sorting.length;i++)
    {
      var temp = sorting[i].split('').sort()//By this the words in the sentence get sorted and i will get the index(number) of the corresponding word at first position.
      console.log(temp);
      var valueHolder = parseInt(temp) //I will take the above obtained value and convert it into integer
        console.log(valueHolder);
      result.splice((valueHolder-1),0,sorting[i]) //will pass the index here
    }
    console.log(result);
  }
功能顺序(字){
var结果=[]
var sorting=words.split(“”).sort()
控制台日志(排序);
对于(设i=0;i
Array#拼接
不是您应该在此处使用的正确方法,而是使用索引将元素添加到所需位置

功能顺序(字){
var结果=[];
var sorting=words.split(“”.sort();
for(设i=0;i顺序(“Fo1r pe6ople 3 ood th5e the 2”);
您可以使用正则表达式,然后可以解析9以上的数字

正则表达式
const r=/\d+/;
查找一个或多个数字。然后在排序方法
a.match(r)
中使用正则表达式,然后解析和比较结果数字

const str='4为第71W条规定的员工提供服务';
功能顺序(字){
让结果=[];
让排序=words.split(“”);
常数r=/\d+/;
排序。排序((a,b)=>{
返回parseInt(a.match(r))-parseInt(b.match(r));
});
console.log(sorting.join(“”));
}

顺序(str);
问题是没有设置结果数组的初始大小,因此在循环期间拼接将无法正常工作

简易修复:

words='4of Fo1r pe6ople g3ood th5e the2';

function order(words){
    var result = []
    var sorting = words.split(' '); 
    var result = new Array(sorting.length);
    console.log(result)
    for(let i=0; i<sorting.length;i++)
    {
      var temp = sorting[i].split('').sort()//By this the words in the sentence get sorted and i will get the index(number) of the corresponding word at first position.
      //console.log(temp);
      var valueHolder = parseInt(temp) //I will take the above obtained value and convert it into integer
        //console.log(valueHolder);
    result.splice(valueHolder-1,1,sorting[i]) //will pass the index here
   
    }
    console.log(result);
  }
  
order(words)
words='4表示员工的健康状况良好;
功能顺序(字){
var结果=[]
变量排序=单词。拆分(“”);
var result=新数组(sorting.length);
console.log(结果)

对于(设i=0;i这将在所有场景中都有效。使用
regex
的代码更少且最简单

解决方案
@Aravinda KS请检查这是否解决了您的问题,如果您有任何问题,请告诉我。
sorting[i].split(“”).sort()
如果我没有弄错的话会生成一个字符数组,它存储在
temp
中。如何将其用作
parseInt
的参数?@TostMaster我特意给出了这一点,因为temp总是给出初始索引值,就像我使用排序一样,所以现在所有的数字都通过使用默认值temp对齐了我们将得到作为初始索引项,这样我就可以得到真正的索引,然后将其转换为integer@NilsKhaler谢谢,伙计,这也很好用
 function order(string) {
   var regex = /\d+/g;
   var matches = string.match(regex);        
   matches.sort(function(a, b){return parseInt(a)-parseInt(b)});

   var splittedArr = string.split(' ');

   var result = [];
   matches.forEach(item => {
      const findItem = splittedArr.find(a => a.includes(item));
      result.push(findItem);
   })
   console.log(result)
}

order('4of Fo1r pe6ople g3ood th5e the2') // your string
order('77of Fo1r pe6ople g3ood th8e the2') // other string
order('0of Fo1r pe6ople g4ood th8e the2') // other string