Order array by predefined rules

后端 未结 4 1546
猫巷女王i
猫巷女王i 2020-12-22 06:29

I have an array of currencies [\"GBP\", \"EUR\", \"NOK\", \"DKK\", \"SKE\", \"USD\", \"SEK\", \"BGN\"]. I would like to order it by moving predefined list if th

4条回答
  •  醉梦人生
    2020-12-22 06:42

    var tabCurrency = ['GBP', 'EUR', 'NOK', 'DKK', 'SKE', 'USD', 'SEK', 'BGN'];

    var tabPredef = ['EUR', 'USD', 'DKK', 'SKE', 'NOK', 'GBP'];

    var newTabGood = [];

        tabPredef.forEach(function (itemPredef, indexPref) {
    
             var indexTemp;
    
             tabCurrency.forEach(function (itemCurrency, indexCurrency) {
                   if(itemPredef == itemCurrency)
                   {
                      newTabGood.push(itemPredef);
                      indexTemp = indexCurrency;
                   }
             })
    
             tabCurrency.splice(indexTemp, 1)
       }) 
    
    
     var resultat = newTabGood.concat(tabCurrency);
    
     console.log(resultat)
    

提交回复
热议问题