2016-09-03 50 views
0

我有一個數組順序排列的如何帶回Java腳本陣列的原有順序

初始順序:

var originalcolumns = [ 
       "es_officer_id", 
       "es_officer_name", 
       "es_officer_fname", 
       "es_bps_title", 
       "es_department_name", 
       "es_bps_id", 
       "es_department_id" 
      ] 

var sortedfirst = originalcolumns.sort(); 

排序sortedfirst陣列後看起來像

[ 
    "es_bps_id", 
    "es_bps_title", 
    "es_department_id", 
    "es_department_name", 
    "es_officer_fname", 
    "es_officer_id", 
    "es_officer_name" 
] 

數組的一個副本(剝去最後一個數值後的值下劃線)

如果返回true,我會將每條記錄匹配到它的下一個記錄,別名將與您在最終結果中看到的相同。

var withoutlast = [ 
       "es_bps", 
       "es_bps", 
       "es_department", 
       "es_department", 
       "es_officer", 
       "es_officer", 
       "es_officer" 
      ] 

var withoutlastsorted = withoutlast.sort(); 

1:它是瞭解我的表名es_officer所以第一個別名應該是與像(a.es_officer_id),這是至今沒有工作,因爲我按字母順序排列的陣列es_officer_id至關重要。

2:相同的首字母必須具有相同的別名,例如:a.es_officer_id將具有與a.es_officer_name相同的別名,在這種情況下,我也碰壁。如果我不對數組進行排序並添加別名,那麼結果前3個結果具有相同的別名,因爲它們是連續的。但去年4將有FOT前不同的別名:BC,d,E

現在增加了一些別名,以每個項目陣列

var alias = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 
        'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; 

var AliasAddedCols = []; 
var aliasIndex = 0; 
var sameColIndex = 0; 

for(var columnName in sortedfirst) 
{ 
    AliasAddedCols.push(alias[aliasIndex] +'.'+sortedfirst[columnName]); 
    if(withoutlast[sameColIndex] !== withoutlast[sameColIndex + 1]) 
    { 
     aliasIndex ++; 
    } 
    sameColIndex++; 
} 

現在如果我安慰AliasAddedCols最終的結果看起來是這樣的:

[ 
    "a.es_bps_id", 
    "a.es_bps_title", 
    "b.es_department_id", 
    "b.es_department_name", 
    "c.es_officer_fname", 
    "c.es_officer_id", 
    "c.es_officer_name" 
] 

唯一的問題是我需要最後一個數組的順序,即「AliasAddedCols」與「原始數組」的順序完全相同。

另外別名從es_bps_id這在我的情況下,它是錯誤的,第一別名起始「一」應與頂部3列(es_officer_id,es_officer_name,es_officer_fname)第二別名「B」應與第4項和6(es_bps_title,es_bps_id)和第三別名「C」應與在陣列(項目5和7 es_department_name,es_department_id

+1

'中的Array.sort()'會產生變異的原始數組,所以你最好開始用'.slice()排序()' – Redu

+1

它是什麼,你真正想實現?如果您不想更改數組的順序,請不要對其排序。 – Bergi

回答

1

我建議下列步驟:

  1. 環繞每個單元素子陣列內的串;
  2. 獲取該數組的淺表副本;
  3. 對字符串部分進行拷貝(就像你做的那樣);
  4. 根據你的邏輯添加前綴(就像你做的那樣);
  5. 步驟1數組中元素的順序沒有變化,所以現在只需再次打開(替換)字符串。

下面是該ES6代碼(但沒有拆解可在功能參數賦值,你發現這是不是在你的環境中支持):

var columns = [ 
 
    "es_officer_id", 
 
    "es_officer_name", 
 
    "es_officer_fname", 
 
    "es_bps_title", 
 
    "es_department_name", 
 
    "es_bps_id", 
 
    "es_department_id" 
 
]; 
 

 
// 0. Define the table name, which should be sorted first 
 
var tablePrefix = 'es_officer_'; 
 
// 1. Convert to array of arrays (to wrap the strings) 
 
columns = columns.map((s, i) => [s]); 
 
// 2. Create a shallow copy 
 
columns.slice() 
 
     // Sort that copy, but keep table name first 
 
     .sort((a, b) => a[0].indexOf(tablePrefix) ? a[0].localeCompare(b[0]) : -1) 
 
     // Alter the strings according to your logic 
 
     .reduce((status, a) => { 
 
      // Chop off last "_word" 
 
      var base = a[0].replace(/_[^_]*$/, ''); 
 
      // When different than previous, increase counter 
 
      if (status.prev !== base) status.charCode++; 
 
      status.prev = base; 
 
      // Prefix string with letter 
 
      a[0] = String.fromCharCode(status.charCode) + '.' + a[0]; 
 
      // Pass status on to next iteration 
 
      return status; 
 
     }, { charCode: 'a'.charCodeAt(0)-1 }); // set initial status 
 
// 3. `columns` still has the original order, now perform the 
 
// inverse of step 1, unwrapping the strings 
 
columns = columns.map((a, i) => a[0]); 
 

 
console.log(columns);

+0

非常感謝你,這正是我需要的。 – Wcan

+0

這很奇怪,它給我錯誤我不知道爲什麼,我在節點api中使用這個。 .sort(([s1],[s2])=> s1.localeCompare(s2))SyntaxError:意外的標記[ – Wcan

+0

好吧,那是因爲ES6沒有完全支持。我將在一分鐘內提供沒有此功能的版本。 – trincot

1

原始陣列只需存儲爲單獨的值,則可以將其轉換爲任何你需要的值是

請注意,Array.prototype.sort()它是否就地工作,這意味着你hav e創建原始數組的副本。您可以使用var copy = myArray.slice()或傳播運營商var copy = [ ...myArray ](ES2015 +)。

1

只是有兩個減少,你可能會做如下;

var columns = [ 
 
       "es_officer_id", 
 
       "es_officer_name", 
 
       "es_officer_fname", 
 
       "es_bps_title", 
 
       "es_department_name", 
 
       "es_bps_id", 
 
       "es_department_id" 
 
      ]; 
 
    sorted = [ 
 
       "0.es_bps_id", 
 
       "1.es_bps_title", 
 
       "2.es_department_id", 
 
       "3.es_department_name", 
 
       "4.es_officer_fname", 
 
       "5.es_officer_id", 
 
       "6.es_officer_name" 
 
      ], 
 
      lut = columns.reduce((m,e,i) => (m[e] = i,m),{}); 
 
     restored = sorted.reduce((res,e) => (res[lut[e.split(/[0-9a-zA-Z]+\./)[1]]] = e,res) ,[]); 
 
console.log(restored);

+0

嘿感謝您的代碼,但我只是犯了一個錯誤,在點之前的數字實際上是單個字母,它可以是隨機的阿爾法,例如,如果有七個項目,那麼它可以是a.es_officer_id,a.es_officer_name, a.es_officer_fname,b.es_bps_title,c.es_department_name,b.es_bps_id,c.es_department_id – Wcan

+0

有一點是常見的,es_bps_id和es_bps_title將具有相同的字母表。 fo ex:b.es_bps_id,b.es_bps – Wcan

+0

然後,你可以像'restored = sorted.reduce((res,e)=>(res [lut [e.split(/ [0-9a-zA-Z] + \ ./)[1]]] = e,res),[]);' – Redu