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数据变量
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);
我的按钮值是“&gt;&gt;”。单击此按钮,javascript navigRight()函数调用...其中第一项arrMiddleDefault被推入arrLeftProp数组。这次第一项是 从arrMiddleDefault数组中删除,并将arrRightProp的第一项插入到arrMiddleDefault数组中。首先点击“&gt;&gt;”按钮console.log(data.prid)给出61494,62998,62999 in navigRight()函数。第二次console.log(data.prid)给出61494,62998,undefined,第三次console.log(data.prid)给出61494,undefined,undefined,第四次console.log(data.prid)给出undefined,undefined,undefined
答案 0 :(得分: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;