I've been experimenting with the unshift function on a multidimensional array, but I'm having trouble getting it to work as expected.
Using shift works fine and does what I need, but unshift doesn't behave properly.
Here is the array I'm working with:
[ [ '487', 'RINGING' ], [ '477', 'RINGING' ] ]
When I try to use unshift on it, it gives me this output:
[ [ '487', 'RINGING' ], [ '477', 'RINGING' ], 2 ]
What I actually want is to move the '477' array to the beginning like this:
[ [ '477', 'RINGING' ], [ '487', 'RINGING' ]]
The code snippet I am using:
var channelArrStatus = [ [ '477', 'RINGING' ], [ '487', 'RINGING' ]];
function monitor_channel(event, channel) {
if (event.device_state['state'] === "RINGING") {
var name = "User_487";
var status = "NOT_INUSE"
var index = 0;
if (channelArrStatus.length === 0) {
var chanar = new Array(name, status);
channelArrStatus.push(chanar);
} else {
var found = false;
for (var i in channelArrStatus) {
var channelArrStatusElem = channelArrStatus[i];
if (channelArrStatusElem[0] === name) {
index = i;
found = true;
if (channelArrStatus[index][1] !== "DND") {
channelArrStatus.push(channelArrStatus.unshift());
setTimeout(function () {
channelArrStatus[index][1] = status;
}, 10000);
}
}
}
}
}
I'm struggling to achieve the desired result of moving an array to the beginning using unshift as shown above.
Any suggestions?
EDIT:JSFiddle