2016-11-03 137 views
1

我想根據用戶輸入生成的字符串不同的參數,它應該是這樣的:1234年至2016年,abc12-3232,我有7個選項,如roll_numberUSER_IDUSER_NAME部門subject_code。這個選項的順序是用戶定義的。例如:用戶可以說我要USER_NAME第一則subject_code然後roll_number等,用戶還可以選擇他多少字符想從一個特定的選項意味着他可以說最後2個字符從roll_number,第一從user_name等3個字符。創建一個字符串基於JavaScript

===我試圖===

首先我拍攝的所有信息在4陣列:

第一個數組是:

var selectedOptions = ['user_name', 'roll_number', 'subject_code', 'class']; 

所以我就我必須爲 字符串創建採取的選項列表。

第二個數組是:

var sequence = [ 
        { key:'user_name', value:2 }, 
        { key:'subject_code', value:3 }, 
        { key:'class', value:1 }, 
        { key:'roll_number', value:4 }, 
       ]; 

此陣列給出選項的序列。

第三陣列是:

var alignment = [ 
      { key:'user_name', value:'left' }, 
      { key:'subject_code', value:'left' }, 
      { key:'class', value:'right' }, 
      { key:'roll_number', value:'left' }, 
     ]; 

它給例如對準的知識如果roll_number是123456和用戶從右然後456應採取所選擇的長度爲3,所以這是左右信息。

第四陣列是:現在使用for loop我得到了

 var user_name = "1234abcd"; 
     var subject_code = "567890"; 
     var class = "2016"; 
     var roll_number = "123"; 

var lengthOfOptions = [ 
      { key:'user_name', value:3 }, 
      { key:'subject_code', value:4 }, 
      { key:'class', value:2 }, 
      { key:'roll_number', value:5 }, 
     ]; 

===接着===

的這個選項的值選項名稱,然後我試圖獲得序列,但我無法獲得特定opti的序列因爲我在for loop裏面,我不確定我應該先獲得序列或選項名稱,如果你有任何簡單的方法或指導方針對我有幫助,我的觀點是我需要運行循環內循環,但我不'你知道如何?謝謝。

== ==編輯

我的for循環代碼

  sequence.sort(function(a, b) { 
       return parseFloat(a.value) - parseFloat(b.value); 
      }); 
      for (var i = 0; i <= sequence.length - 1; i++){ 
       console.log("The key is :", sequence[i]['key']); 
       console.log("The value is :", sequence[i]['value']); 
      } 

在此之後要做什麼,我無法理解。

+0

嗨,你可以張貼的代碼爲'loop',好嗎? – Andrea

回答

1

首先,你可以排序sequence然後用Array.prototype.reduce來計算每個selectedOptionslengthalignment並將其連接到所需字符串:

下面3210

演示:

var selectedOptions = ['user_name', 'roll_number', 'subject_code', 'class']; 
 
var sequence = [{ key:'user_name', value:2 },{ key:'subject_code', value:3 },{ key:'class', value:1 },{ key:'roll_number', value:4 },]; 
 
var alignment = [{ key:'user_name', value:'left' },{ key:'subject_code', value:'left' },{ key:'class', value:'right' },{ key:'roll_number', value:'left' },]; 
 
var lengthOfOptions = [{ key:'user_name', value:3 },{ key:'subject_code', value:4 },{ key:'class', value:2 },{ key:'roll_number', value:5 },]; 
 

 
// input values 
 
var options={user_name:"1234abcd",subject_code:"567890", class:"2016",roll_number:"123"}; 
 

 
var result = sequence.sort(function(a, b) { 
 
    return a.value - b.value; 
 
}).reduce(function(prev, curr) { 
 
    // if sequence elements are selected options 
 
    if (selectedOptions.indexOf(curr.key) !== -1) { 
 
    // find length and alignment 
 
    var length = Math.min(lengthOfOptions.filter(e => e.key == curr.key)[0].value, options[curr.key].length); 
 
    var align = alignment.filter(e => e.key == curr.key)[0].value; 
 

 
    // extract the substring from input 
 
    if (align == "left") { 
 
     prev.push(options[curr.key].slice(0, length)); 
 
    } else { 
 
     prev.push(options[curr.key].slice(-length)); 
 
    } 
 
    return prev; 
 
    } 
 
}, []).join('-'); 
 

 
console.log(result);

注意在計算長度使用的Math.min - 檢查是否lengthOption超過輸入值的大小。

編輯

所以,如果你想放的0一個padding萬一lengthOption比輸入值,你可以用這個。乾杯!

var selectedOptions = ['user_name', 'roll_number', 'subject_code', 'class']; 
 
var sequence = [{ key:'user_name', value:2 },{ key:'subject_code', value:3 },{ key:'class', value:1 },{ key:'roll_number', value:4 },]; 
 
var alignment = [{ key:'user_name', value:'left' },{ key:'subject_code', value:'left' },{ key:'class', value:'right' },{ key:'roll_number', value:'left' },]; 
 
var lengthOfOptions = [{ key:'user_name', value:3 },{ key:'subject_code', value:4 },{ key:'class', value:2 },{ key:'roll_number', value:5 },]; 
 

 
var options={user_name:"1234abcd",subject_code:"567890", class:"2016",roll_number:"123"}; 
 

 
var result = sequence.sort(function(a, b) { 
 
    return a.value - b.value; 
 
}).reduce(function(prev, curr) { 
 
    if (selectedOptions.indexOf(curr.key) !== -1) { 
 
    var lengthOption = lengthOfOptions.find(e => e.key == curr.key).value; 
 
    var align = alignment.find(e => e.key == curr.key).value; 
 
    var padding = Math.max(lengthOption - options[curr.key].length, 0); 
 
    if (align == "left") { 
 
     prev.push(Array(padding + 1).join(0) + options[curr.key].slice(0, Math.min(lengthOption, options[curr.key].length))); 
 
    } else { 
 
     prev.push(Array(padding + 1).join(0) + options[curr.key].slice(-Math.min(lengthOption, options[curr.key].length))); 
 
    } 
 
    return prev; 
 
    } 
 
}, []).join('-'); 
 

 
console.log(result);

+0

@ArpitMeena讓我知道這是否適合你,謝謝! – kukkuz

+0

該代碼爲我工作,感謝它。我試圖在這段代碼中做一些修改,就像你在這個虛擬例子中看到的** roll_number **我說的長度應該是'5',但我只有'3'字符,在這種情況下輸出只顯示爲'123',但它應該是'00123',所以我想我需要在'if'條件對齊後再放置一個'if'條件,我是對不對? –

+0

@ArpitMeena這可能會發生任何輸入值,不僅是'roll_number' ...所以你想要爲所有的前綴零? – kukkuz

1

在你的情況下,roll_number將是空的。因此,你會看到一個連字符在最後字符串的結尾

var selectedOptions = ['user_name', 'roll_number', 'subject_code', 'class']; 
 
var sequence = [{ 
 
    key: 'user_name', 
 
    value: 2 
 
}, { 
 
    key: 'subject_code', 
 
    value: 3 
 
}, { 
 
    key: 'class', 
 
    value: 1 
 
}, { 
 
    key: 'roll_number', 
 
    value: 4 
 
}, ]; 
 

 
var alignment = [{ 
 
    key: 'user_name', 
 
    value: 'left' 
 
}, { 
 
    key: 'subject_code', 
 
    value: 'left' 
 
}, { 
 
    key: 'class', 
 
    value: 'right' 
 
}, { 
 
    key: 'roll_number', 
 
    value: 'left' 
 
}, ]; 
 

 
var lengthOfOptions = [{ 
 
    key: 'user_name', 
 
    value: 3 
 
}, { 
 
    key: 'subject_code', 
 
    value: 4 
 
}, { 
 
    key: 'class', 
 
    value: 2 
 
}, { 
 
    key: 'roll_number', 
 
    value: 5 
 
}, ]; 
 

 
var object = { 
 
    "user_name": "1234abcd", 
 
    "subject_code": "567890", 
 
    "class": "2016", 
 
    "roll_number": "123" 
 
} 
 

 
var result = []; 
 

 
//Iterate each option 
 
for (var i = 0; i < selectedOptions.length; i++) { 
 
    var key = selectedOptions[i]; 
 
    var seq = sequence.filter(function(v, i) { 
 
    return v.key === key; 
 
    })[0].value; //Get the sequence number 
 
    var align = alignment.filter(function(v, i) { 
 
    return v.key === key; 
 
    })[0].value; //Get the alignement value 
 
    var length = lengthOfOptions.filter(function(v, i) { 
 
    return v.key === key; 
 
    })[0].value; //Get the length value 
 

 
    var string = object[key]; //Get the string to modify 
 
    if (align === "left") { 
 
    string = string.slice(length); 
 
    } else { 
 
    string = string.slice(0, string.length - length); 
 
    } 
 

 
    result[seq - 1] = string; //Add the string in an array at the correct sequence index. 
 
} 
 

 
var finalString = result.join("-"); //Concat all the strings with '-' 
 

 
console.log(finalString);

相關問題