Order array by predefined rules

后端 未结 4 1574
猫巷女王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:57

    I guess this can also be achieved like this

    Array.prototype.intersect = function(a) {
      return this.filter(e => a.includes(e));
    };
    Array.prototype.excludes = function(a) {
      return this.filter(e => !a.includes(e));
    };
    var getCur = (p,c) => p.intersect(c).concat(c.excludes(p)),
          cur1 = ["GBP", "EUR", "NOK", "DKK", "SKE", "USD", "SEK", "BGN"],
          cur2 = ["GBP", "EUR", "NOK", "LTU", "ZGN"],
           pdl = ['EUR', 'USD', 'DKK', 'SKE', 'NOK', 'GBP', 'SEK', 'BGN'];
    console.log(getCur(pdl,cur1));
    console.log(getCur(pdl,cur2));

提交回复
热议问题