2013-04-04 83 views
-3
var arrPropertiesAndChannels = null, arrPrrp = null, arrLeftProp = null, arrMiddleDefault = null, arrRightProp = null; 
     $(document).ready(function() { 
      arrPropertiesAndChannels = { "props": [{ "prid": "61494", "prname": "Park Plaza Hotel Gurgaon", "pos": "1", "pinned": false, "display": true }, { "prid": "62998", "prname": "Lemon Tree City Center", "pos": "2", "pinned": false, "display": true }, { "prid": "62999", "prname": "Lemon Tree Hotel Udyog Vihar", "pos": "3", "pinned": false, "display": true }, { "prid": "78097", "prname": "Royal Retreat", "pos": "4", "pinned": false, "display": false }, { "prid": "78098", "prname": "SkRetreat", "pos": "5", "pinned": false, "display": false }, { "prid": "78099", "prname": "VBkRetreat", "pos": "6", "pinned": false, "display": false}], "chans": [{ "chid": "1", "chname": "FACEBOOK", "pos": "1" }, { "chid": "2", "chname": "TWITTER", "pos": "2" }, { "chid": "3", "chname": "NEWSLETTER", "pos": "3"}] }; 
      arrPrrp = arrPropertiesAndChannels.props; 
      arrLeftProp = arrPrrp.slice(0, 0); 
      arrMiddleDefault = arrPrrp.slice(0, 3); 
      arrRightProp = arrPrrp.slice(3, 8); 

     }); 


function navigRight() { 
    var tempprid = ''; 



    $.each(arrMiddleDefault, function (i, data) { 
     console.log(data.prid); 
     if (data.pinned == false) { 
      arrLeftProp.push(data); 
      tempprid = data.prid; 
      //console.log(tempprid); 
      //break; 
     } 
    }); 



arrMiddleDefault = arrMiddleDefault.filter(function (el) { 
     return el.prid !== tempprid; 
    }); 

    arrMiddleDefault.push(arrRightProp.slice(0, 1)); 
    //console.log(arrMiddleDefault); 
    //console.log(arrLeftProp); 


    arrRightProp = arrRightProp.filter(function (el) { 
     return el.prid !== arrRightProp.slice(0, 1)[0].prid; 
    }); 

} 

<div> 
     <input type="button" value="<<" onclick="navigLeft()"> 
     <div id="dvTest"> 
     </div> 
     <input type="button" value=">>" onclick="navigRight()"> 
    </div> 

動態數組我有JSON數據可變遍歷以JavaScript

arrPropertiesAndChannels ={ "props": [{ "prid": "61494", "prname": "Park Plaza Hotel Gurgaon", "pos": "1", "pinned": false, "display": true }, { "prid": "62998", "prname": "Lemon Tree City Center", "pos": "2", "pinned": false, "display": true }, { "prid": "62999", "prname": "Lemon Tree Hotel Udyog Vihar", "pos": "3", "pinned": false, "display": true }, { "prid": "78097", "prname": "Royal Retreat", "pos": "4", "pinned": false, "display": false }, { "prid": "78098", "prname": "SkRetreat", "pos": "5", "pinned": false, "display": false }, { "prid": "78099", "prname": "VBkRetreat", "pos": "6", "pinned": false, "display": false}], "chans": [{ "chid": "1", "chname": "FACEBOOK", "pos": "1" }, { "chid": "2", "chname": "TWITTER", "pos": "2" }, { "chid": "3", "chname": "NEWSLETTER", "pos": "3"}] }; 

,使我們有

arrPrrp = arrPropertiesAndChannels.props; 
arrLeftProp = arrPrrp.slice(0, 0); 
console.log(arrLeftProp); 
arrMiddleDefault = arrPrrp.slice(0, 3); 
console.log(arrMiddleDefault); 
arrRightProp = arrPrrp.slice(3, 8); 
console.log(arrRightProp); 

我有一個按鈕值是 「>>」。點擊這個按鈕,javascript navigRight()函數調用...其中arrMiddleDefault的第一項被壓入arrLeftProp數組中。此時第一項是從arrMiddleDefault數組中刪除的 ,並且第一項arrRightProp被插入到arrMiddleDefault數組中。首先點擊按鈕「>>」console.log(data.prid),在 navigRight()函數中給出61494,62998,62999。第二次console.log(data.prid)給出了61494,62998,undefined,第三次console.log(data.prid)給出了61494,undefined,undefined,第四次console.log(data.prid)給出了undefined,undefined,undefined

+0

現在檢查..我解釋了這個..使用console.log在firbug中檢查您的HTML頁面。 – Shailendra 2013-04-04 18:45:04

回答

0

問題是,當你撥打:

arrMiddleDefault.push(arrRightProp.slice(0, 1)); 

您正在推動一個陣列到陣列arrMiddleDefault,當你真的想要的第一個元素推到arrMiddleDefault陣列。

你也可以使用這樣的:

arrMiddleDefault.push(arrRightProp.slice(0, 1)[0]); 

但它也可以同樣被縮短到這一點:同樣

arrMiddleDefault.push(arrRightProp[0]); 

,在那裏你具備以下條件:

return el.prid !== arrRightProp.slice(0, 1)[0].prid; 

你可以縮短到:

return el.prid !== arrRightProp[0].prid; 
+0

感謝它給出了正確的答案。 – Shailendra 2013-04-05 03:52:17