2016-02-29 102 views
0

我一直在尝试对MD数组使用unshift函数,并且我无法使它相应地工作。对多维数组使用unshift函数

我正在使用shift没关系,它确实需要像预期的那样,但不转移不。

下面是我使用的阵列:

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

当我尝试在它上面做一个不印字它显示以下内容:

[ [ '487', 'RINGING' ], [ '477', 'RINGING' ], 2 ] 

我只需要477阵列转移到开始像这样:

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

的代码我使用:

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

我不能让它将数组移动到数组的开头,如上面使用unshift突出显示的那样。

有什么建议吗?

编辑:JSFiddle

+0

'unshift'将一个元素添加到数组的开头,你试图实现什么? – giannisf

+0

@giannisf多数民众赞成是什么试图做,但我不能得到它的工作.. – Studento919

+0

看看我的回答 – giannisf

回答

0

呼叫.unshift()预先考虑什么并返回数组的长度 - 在你的情况2 - 这就是你正在推动的价值。

要么你想

channelArrStatus.push(channelArrStatus.shift()); // no un- 

channelArrStatus.unshift(channelArrStatus.pop()); 

这就是说,你应该use literal notation instead of the Array constructoravoid for in enumerations on arrays

+0

我不想将它移动到数组的末尾我需要将其移动到开始,我已经添加了一个JS的小提琴演奏这个使用'channelArrStatus.unshift(channelArrStatus.pop())演示这个问题;'但没有用 – Studento919

+0

Nevermind明白了:) – Studento919

0

在此行中

channelArrStatus.push(channelArrStatus.unshift()); 

您正在添加数组的长度,以阵列的。 如果你想添加一个元素到数组的开头,只需调用unshift

channelArrStatus.unshift(element); 
+0

没有它的喜悦我也试过了下面的答案我已经更新了JS小提琴,如果有帮助。 – Studento919