jsÔõôɾ³ýÊý×éÖеÄij¸öÔªËØ
javascript ɾ³ýÊý×éÔªËصÄÒªÁìÓУº1. splice() ɾ³ýÖ¸¶¨Ë÷ÒýµÄÔªËز¢·µ»Øɾ³ýµÄÔªËØ£»2. pop() ɾ³ý²¢·µ»Ø×îºóÒ»¸öÔªËØ£»3. shift() ɾ³ý²¢·µ»ØµÚÒ»¸öÔªËØ£»4. filter() ͨ¹ý»Øµ÷º¯Êýɸѡ²»Öª×ãÌõ¼þµÄÔªËز¢·µ»ØÐÂÊý×é¡£
ÔõÑùÓà JavaScript ɾ³ýÊý×éÖеÄij¸öÔªËØ
JavaScript ÌṩÁ˶àÖÖÒªÁìÀ´É¾³ýÊý×éÖеÄij¸öÔªËØ¡£ÒÔÏÂÊÇ×î³£ÓõÄÒªÁ죺
1. splice() ÒªÁì
splice() ÒªÁì¿ÉÒÔÒ»´ÎÐÔɾ³ýÊý×éÖÐÒ»¸ö»ò¶à¸öÔªËØ£¬²¢·µ»ØÒ»¸ö°üÀ¨ÒÑɾ³ýÔªËصÄÐÂÊý×é¡£Óï·¨ÈçÏ£º
array.splice(start, deleteCount)
µÇ¼ºó¸´ÖÆ
ÆäÖУº
start£ºÒª×îÏÈɾ³ýÔªËصÄË÷Òý¡£
deleteCount£ºÒªÉ¾³ýµÄÔªËØÊýÄ¿¡£
ÀýÈ磬ɾ³ýÊý×é [1, 2, 3, 4, 5] ÖÐË÷ÒýΪ 2 µÄÔªËØ£º
const arr = [1, 2, 3, 4, 5]; arr.splice(2, 1); // ɾ³ýË÷ÒýΪ 2 µÄÔªËØ console.log(arr); // [1, 2, 4, 5]
µÇ¼ºó¸´ÖÆ
2. pop() ÒªÁì
pop() ÒªÁì´ÓÊý×éµÄĩβɾ³ý×îºóÒ»¸öÔªËØ£¬²¢·µ»Ø¸ÃÔªËØ¡£Óï·¨ÈçÏ£º
array.pop()
µÇ¼ºó¸´ÖÆ
ÀýÈ磬ɾ³ýÊý×é [1, 2, 3] ÖеÄ×îºóÒ»¸öÔªËØ£º
const arr = [1, 2, 3]; const removedElement = arr.pop(); // ɾ³ý²¢·µ»ØÊý×éµÄ×îºóÒ»¸öÔªËØ console.log(arr); // [1, 2] console.log(removedElement); // 3
µÇ¼ºó¸´ÖÆ
3. shift() ÒªÁì
shift() ÒªÁì´ÓÊý×éµÄ¿ªÍ·É¾³ýµÚÒ»¸öÔªËØ£¬²¢·µ»Ø¸ÃÔªËØ¡£Óï·¨ÈçÏ£º
array.shift()
µÇ¼ºó¸´ÖÆ
ÀýÈ磬ɾ³ýÊý×é [1, 2, 3] ÖеĵÚÒ»¸öÔªËØ£º
const arr = [1, 2, 3]; const removedElement = arr.shift(); // ɾ³ý²¢·µ»ØÊý×éµÄµÚÒ»¸öÔªËØ console.log(arr); // [2, 3] console.log(removedElement); // 1
µÇ¼ºó¸´ÖÆ
4. filter() ÒªÁì
filter() ÒªÁ콨ÉèÒ»¸ö°üÀ¨Öª×ã¸ø¶¨Ìõ¼þµÄÐÂÊý×飬ÆäÖв»°üÀ¨²»Öª×ãÌõ¼þµÄÔªËØ¡£Óï·¨ÈçÏ£º
array.filter(callbackFunction)
µÇ¼ºó¸´ÖÆ
ÆäÖУº
callbackFunction£ºÒ»¸ö²âÊÔÿ¸öÔªËØÊÇ·ñÖª×ãÌõ¼þµÄº¯Êý¡£
ÀýÈ磬ɾ³ýÊý×é [1, 2, 3, 4] ÖÐËùÓÐżÊý£º
const arr = [1, 2, 3, 4]; const evenNumbers = arr.filter(num => num % 2 === 0); console.log(evenNumbers); // [2, 4]
µÇ¼ºó¸´ÖÆ
ÒÔÉϾÍÊÇjsÔõôɾ³ýÊý×éÖеÄij¸öÔªËصÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡