jsÖÐÔõÑùɾ³ýÊý×éÔªËØ
javascript ÖÐÓÐÈýÖÖÒªÁì¿Éɾ³ýÊý×éÔªËØ£ºÊ¹Óà splice() ÒªÁ죬ָ¶¨É¾³ýÔªËصÄÆðʼË÷ÒýºÍÊýÄ¿£¬²¢·µ»ØÒÑɾ³ýÔªËØ¡£Ê¹Óà pop() ÒªÁìɾ³ý²¢·µ»Ø×îºóÒ»¸öÔªËØ¡£Ê¹Óà shift() ÒªÁìɾ³ý²¢·µ»ØµÚÒ»¸öÔªËØ¡£
ÔõÑùÔÚ JavaScript ÖÐɾ³ýÊý×éÔªËØ
ʹÓà splice() ÒªÁì
splice() ÒªÁì¿ÉÒÔÖ±½Óɾ³ýÊý×éÖеÄÔªËØ£¬²¢·µ»Ø±»É¾³ýµÄÔªËØ¡£Óï·¨ÈçÏ£º
let removedElements = array.splice(start, deleteCount);
µÇ¼ºó¸´ÖÆ
start£ºÒª×îÏÈɾ³ýÔªËصÄË÷ÒýλÖá£
deleteCount£ºÒªÉ¾³ýµÄÔªËØÊýÄ¿¡£
ʾÀý£º
const fruits = ['Æ»¹û', 'Ï㽶', 'Àæ', 'ÆÏÌÑ']; const removedElements = fruits.splice(1, 2); // ´ÓË÷Òý 1 ×îÏÈɾ³ýÁ½¸öÔªËØ console.log(fruits); // ['Æ»¹û', 'ÆÏÌÑ'] console.log(removedElements); // ['Ï㽶', 'Àæ']
µÇ¼ºó¸´ÖÆ
ʹÓà pop() ÒªÁì
pop() ÒªÁìɾ³ý²¢·µ»ØÊý×éµÄ×îºóÒ»¸öÔªËØ¡£Óï·¨£º
let lastElement = array.pop();
µÇ¼ºó¸´ÖÆ
ʾÀý£º
const fruits = ['Æ»¹û', 'Ï㽶', 'Àæ', 'ÆÏÌÑ']; const lastElement = fruits.pop(); // ɾ³ý²¢·µ»Ø×îºóÒ»¸öÔªËØ console.log(fruits); // ['Æ»¹û', 'Ï㽶', 'Àæ'] console.log(lastElement); // 'ÆÏÌÑ'
µÇ¼ºó¸´ÖÆ
ʹÓà shift() ÒªÁì
shift() ÒªÁìɾ³ý²¢·µ»ØÊý×éµÄµÚÒ»¸öÔªËØ¡£Óï·¨£º
let firstElement = array.shift();
µÇ¼ºó¸´ÖÆ
ʾÀý£º
const fruits = ['Æ»¹û', 'Ï㽶', 'Àæ', 'ÆÏÌÑ']; const firstElement = fruits.shift(); // ɾ³ý²¢·µ»ØµÚÒ»¸öÔªËØ console.log(fruits); // ['Ï㽶', 'Àæ', 'ÆÏÌÑ'] console.log(firstElement); // 'Æ»¹û'
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇjsÖÐÔõÑùɾ³ýÊý×éÔªËصÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡
ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£