简体   繁体   中英

Overwrite/Replace Value in multi-dimensional array

So I am currently trying to overwrite a value in my multidimensional array.....where I want to overwrite a value based on the position of where channel.name is and change the value of the mute state if you will.

So what I want to do is:

  1. Mute value in array
  2. When I call the function to unmute it, I want to replace the "Mute" Value with "Unmute".
  3. Then do the same Mute and effectively toggle values.

The problem is I am currently always pushing the values on over and over like so: [ [ '1447682617.29', 'SIP/487-0000000b', 'Mute', 'Unmute' ] ]

I simply just want to overwrite Mute and Replace it with Unmute.

I have the index part fine its just being able to overwrite that value.

The code I have so far is:

//ARI function to mute channels.
  function mute(mutval,mutestate) {
    console.log("Muting:" + mutval);
    var index = 0;
    var findex = false;
    for (var i in channelArr) {
      var channelArrEle = channelArr[i];
      if (channelArrEle[0] == mutval) {
        index = i;
        findex = true;
        console.log("First Part works!")
      }
    }
    if (findex) {
      channelArr[index].push(mutestate);
      console.log(channelArr);
    } else {
      console.log("Error")
    }
  }

  //ARI function to unmute channels which have been muted.
  function unmute(unval,mutestate) {
    console.log("Unmuting: " + unval);
            var index = 0;
    var findex = false;
    for (var i in channelArr) {
      var channelArrEle = channelArr[i];
      if (channelArrEle[0] == unval) {
        index = i;
        findex = true;
        console.log("First Part works!")
      }
    }
    if (findex) {
      channelArr[index].push(mutestate);
      //Just added this to see what would happen!
      channelArr[index] = mutestate;
      console.log(channelArr); 
    } else {
      console.log("Error")
    }
  }

Any suggestions no doubt its something minor question is what.

Try change (in both occurence)

channelArr[index].push(mutestate);

with

var channelData = channelArr[index];
channelData[2] = mutestate;
channelArr[index] = channelData;

And if it works, you can try to shorten the code like this

channelArr[index][2] = mutestate;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM