Studento919
Studento919

Reputation: 635

Find array and move it to the beginning of a Multidimesional array

I have been trying to develop a way to move an array to the beginning of an array.

The issue am having is that I was using as suggested in a previous question:

channelArrStatus.unshift(channelArrStatus.pop());

Issue being it will only ever remove the last value and stick it at the beginning.

But if the values are different every time I need it to locate that if the condition was met then move the array which met the conditions to the start of the array.

What I need to happen

Before Find - [ '477', 'RINGING' ] :

[ [ '487', 'RINGING' ], [ '477', 'RINGING' ],[ '488', 'RINGING' ] ]

After:

[[ '477', 'RINGING' ],[ '487', 'RINGING' ],[ '488', 'RINGING' ] ]

What is currently happening!

Before:

[ [ '487', 'RINGING' ], [ '477', 'RINGING' ],[ '488', 'RINGING' ] ]

After:

[ [ '488', 'RINGING' ],[ '487', 'RINGING' ], [ '477', 'RINGING' ] ]

Ideally when a condition has been met it should move that array to the start I have made a JSFiddle & Code is below as an example:

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();

JSFiddle Example

I know why it isnt working but I have tried mostly everything to move the array it finds to the beginning.

Any Ideas?

Upvotes: 1

Views: 42

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386756

This proposal uses a function for the lookup and returns the index for splicing and unshifting.

var data = [['487', 'RINGING'], ['477', 'RINGING'], ['488', 'RINGING']],
    index = function (array, search) {
        var index = -1;
        array.some(function (a, i) {
            if (a.every(function (b, j) { return b === search[j]; })) {
                index = i;
                return true;
            }
        });
        return index;
    }(data, ['477', 'RINGING']);

~index && data.unshift(data.splice(index, 1)[0]);
document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');

Upvotes: 1

vbarbarosh
vbarbarosh

Reputation: 3702

Use unshift with splice:

var input = ['aa', 'bb', 'cc', 'dd'],
    index = input.indexOf('cc');
Array.prototype.unshift.apply(input, input.splice(index, 1));

Upvotes: 1

Related Questions