I've been attempting to figure out a solution for moving a specific array within another array to the beginning.
The problem I'm encountering is that the code I was using, as suggested in a previous question, only removes the last value and places it at the front:
channelArrStatus.unshift(channelArrStatus.pop());
The issue arises when the values are different each time; in such cases, I need it to identify which array meets certain conditions and move that particular array to the start of the main array.
Desired Outcome
Initial Array - [ '477', 'RINGING' ] :
[ [ '487', 'RINGING' ], [ '477', 'RINGING' ],[ '488', 'RINGING' ] ]
Resulting Array:
[[ '477', 'RINGING' ],[ '487', 'RINGING' ],[ '488', 'RINGING' ] ]
Current Behavior!
Before Reordering:
[ [ '487', 'RINGING' ], [ '477', 'RINGING' ],[ '488', 'RINGING' ] ]
After Reordering:
[ [ '488', 'RINGING' ],[ '487', 'RINGING' ], [ '477', 'RINGING' ] ]
What's happening now doesn't match my intended logic. Here's a simplified version of the code:
var channelArrStatus = [ [ '477', 'RINGING' ], [ '487', 'NOT_INUSE' ], [ '488', 'RINGING' ]];
var state = "NOT_INUSE";
function testArray(){
if (state === "NOT_INUSE") {
var name = "487";
var status = "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") {
setTimeout(function () {
channelArrStatus[index][1] = status;
if(channelArrStatus[index][1] === status){
channelArrStatus.unshift(channelArrStatus.pop());
document.write(channelArrStatus);
}
}, 4000);
}
}
}
}
}
}
testArray();
I understand why it's not working as expected, and I've tried various approaches to rearrange the identified array to the front. Any suggestions?