JavaScript:根据数组项中的数字对数组重新排序

时间:2018-11-14 16:56:52

标签: javascript arrays

我正在尝试解读一个数组。数组中的字符串项目乱序,并附带一个数字,以指定顺序。我想在数组项中获取数字并重新分配数组项索引以反映该数字。例如var scrambled = ["pizza4", "to2", "I0", "eat3", "want1"]。我有一个函数,在数组中搜索附加的数字并返回值。现在,我想将返回的值转换为新数组(如var unscrambled = []中新项目的索引。这是我到目前为止的内容:

function unscramblePhrase() {
    var scrambled = ["pizza4", "to2", "I0", "eat3", "want1"];   
    var unscrambled = [];
    for (var counter = 0; counter < scrambled.length; counter++) {
        numPosition = scrambled[counter].search('[0-9]');
        arrayIndex = scrambled[counter].substring(numPosition);
        console.log(arrayIndex);
        unscrambled.push(scrambled[arrayIndex]);
    }
 console.log(unscrambled)
}

我看到我的arrayIndex正在从加扰的数组项的末尾提取数字,但是我尝试根据此变量分配索引位置的结果是产生了一个新的加扰的数组:["want1", "I0", "pizza4", "eat3", "to2"]

6 个答案:

答案 0 :(得分:2)

这不是对数组进行排序的最佳方法,但是您的问题是使用Array.prototype.push时,应该将值分配给unscrambled中的索引。

unscrambled[arrayIndex] = scrambled[counter];

但是真正做到这一点的方法是使用Array.prototype.sort

function getNum(str){
    return Number(str.substring(str.length -1));
}


unscrambled.sort((a, b) => getNum(a) - getNum(b));
  

注意:此方法对数组in-place进行排序。根据您的要求,这可能是好事,或者不是好事

     

但是您始终可以在克隆上使用它:

[...unscrambled].sort((a, b) => getNum(a) - getNum(b));

答案 1 :(得分:2)

您可以使用RegEx将数组拆分为值和索引,对它们进行排序,然后使用辅助.map删除附加信息以返回字符串数组,从而简化此过程。

scrambled.map(i => 
[i.replace(/\d/g,""), +i.replace(/\D/g,"")])
.sort((a, b) => a[1] - b[1])).map(i => i[0]);

var scrambled = ["pizza4", "to2", "I0", "eat3", "want1"];   

var unscrambled = scrambled.map(i => 
  [i.replace(/\d/g,""), +i.replace(/\D/g,"")])
  .sort((a, b) => a[1] - b[1])
  .map( i => i[0]);

console.log(unscrambled);

答案 2 :(得分:1)

您可以执行一个循环并获取字符串和索引的一部分,将值分配给索引并返回数组。

var scrambled = ["pizza4", "to2", "I0", "eat3", "want1"],
    result = scrambled.reduce((r, string) => {
        var [s, i] = string.match(/\D+|\d+/g);
        r[i] = s;
        return r;
    }, []);
    
console.log(result);

更有趣的对象。

var scrambled = ["pizza4", "to2", "I0", "eat3", "want1"],
    result = Object.assign(
        [],
        ...scrambled.map(s => (([v, k]) => ({ [k]: v }))(s.match(/\D+|\d+/g)))
    );
    
console.log(result);

答案 3 :(得分:0)

尝试一下(如果可行,则不知道):

function unscramblePhrase() { var scrambled = ["pizza4", "to2", "I0", "eat3", "want1"];
var unscrambled = []; for (var counter = 0; counter < scrambled.length; counter++) { numPosition = scrambled[counter].search('[0-9]'); arrayIndex = scrambled[counter].substring(numPosition); console.log(arrayIndex); unscrambled[arrayIndex] = scrambled[counter]; } console.log(unscrambled) }

答案 4 :(得分:0)

您可以尝试使用“ Array.sort”,如下所示

var scrambled = ["pizza4", "to2", "I0", "eat3", "want1"]

let getNumberIndex = (d) => [...d].findIndex(v => Number.isInteger(+v))

let getNumber = (d) => d.slice(getNumberIndex(d))

let unscrambled = scrambled.slice(0).sort((a,b) => getNumber(a) - getNumber(b))

console.log(unscrambled)

答案 5 :(得分:0)

const words = ["I0", "want1", "to2", "eat3", "pizza4"]

words
    .map (w => parseInt (w [--w.length]))
    .sort ()
    .map (i => words.filter (w => w [--w.length] == i) [0].replace (i, ''))

// ["I", "want", "to", "eat", "pizza"]