2016-07-07 46 views
-2

這是陣列如何拼接基於i通過,並與剩餘的元素

pickelement  = ["2123","149","3096","1471"]; 
var shiftlength = 4; //this is also dynamic 

從上述我需要在循環拼接也重新初始化數組的長度的陣列,長度拼接將是動態的

for(var s = 0;s<shiftlength;s++){ 

nofofillres = Ext.ComponentQuery.query('[name=Resources'+s+']'[shiftreslength].value.length; 

searchInput = pickelement .splice(nofofillres); // this is not working as expected 
console.log(searchInput); // Each time same number of elements is coming 

// I need an answer suppose if 
nofofillres for 1st iteration is 1 and second is 1 and third is 1 and fourth is 1 Then 
spliced element should be "2123" 
spliced element should be "149" 
spliced element should be "3096" 
spliced element should be "1471" 
and also each time when sliced pickelement should be reinitalzied as below 
["149","3096","1471"] 
["3096","1471"]; 
["1471"]; 
Please provide an answer m stuck with it 

} 
+1

可以pleasse提供您預期的結果的原代碼? –

+0

嗨,我的預期輸出應該在一個變量它應該得到拼接元素和其餘元素應該重新初始化並執行相同的循環 –

+0

你可以寫你想要的輸出。或者只是舉一個例子,並顯示你想要的結果。 – Vipul

回答

1

請檢查如果這是你在找什麼。我已經編輯根據您的評論

var pickelement = ["2123","149","3096","1471"]; 
    var shiftlength = 4; 
    for(var s = 0;s<shiftlength;s++){ 
     var nofofillres = 1; 
     var splicedArray = pickelement.splice(0,nofofillres); 
     console.log("Spliced Array of Iteration : " + (s+1) + " is "); 
     console.log(splicedArray); 
     console.log("pickelement after Iteration : " + (s+1) + " is "); 
     console.log(pickelement); 
    } 

輸出

Spliced Array of Iteration : 1 is 
    ["2123"] 
    pickelement after Iteration : 1 is 
    ["149", "3096", "1471"] 
    Spliced Array of Iteration : 2 is 
    ["149"] 
    pickelement after Iteration : 2 is 
    ["3096", "1471"] 
    Spliced Array of Iteration : 3 is 
    ["3096"] 
    pickelement after Iteration : 3 is 
    ["1471"] 
    Spliced Array of Iteration : 4 is 
    ["1471"] 
    pickelement after Iteration : 4 is 
    [] 
+0

嗨,對不起,我不是尋找任何偶數,我正在尋找ResString = [2123,140] //如果nofres是2即選擇el的長度如果noof res爲1,則進行下一次迭代,則ResString = [3096] –

+0

請查看編輯後的代碼是否是您要查找的內容。 – 2016-07-07 05:56:16

+0

嗨Jaganathan Nanthakumar,這是我在找什麼,會嘗試這個和發佈,如果工作正常。謝謝 –