Removing elements from JavaScript arrays

后端 未结 5 2059
别跟我提以往
别跟我提以往 2021-02-10 22:31

I have the following array setup, i,e:

var myArray = new Array();

Using this array, I am creating a breadcrumb menu dynamically as the user add

5条回答
  •  独厮守ぢ
    2021-02-10 22:49

    You could use myArray.push('MenuA'); so you don't specify direct numbers when adding elements.

    To remove an element I.E. 'MenuB':

    // another quick way to define an array
    myArray = ['MenuA', 'MenuB', 'MenuC', 'MenuD', 'MenuE']; 
    
    // remove an item by value:
    myArray.splice(myArray.indexOf('MenuB'),1);
    
    // push a new one on
    myArray.push('MenuZ');
    
    // myArray === ["MenuA", "MenuC", "MenuD", "MenuE", "MenuZ"]
    

提交回复
热议问题