×ðÁú¿­Ê±¹ÙÍøµÇ¼

jsÔõôɾ³ýÊý×éÖеÄijһÏî

ÓÐÈýÖÖ´Ó javascript Êý×éÖÐɾ³ýÒ»ÏîµÄÒªÁ죺ʹÓà splice() ÒªÁìÔÚÖ¸¶¨Î»ÖÃɾ³ýÏʹÓà filter() ÒªÁ콨ÉèÐÂÊý×é £¬½ö°üÀ¨ÇкÏÌõ¼þµÄÏʹÓà slice() ºÍ concat() ÒªÁ츴ÖÆËùÐèÏîµ½ÐÂÊý×éÖС£

ÔõÑùÔÚJavaScriptÖдÓÊý×éÖÐɾ³ýÒ»Ïî

ÒªÁìÒ»£ºÊ¹ÓÃsplice()ÒªÁì

splice()ÒªÁì¿ÉÒÔɾ³ýÊý×éÖÐÖ¸¶¨Î»ÖõÄÏî £¬²¢¿Éƾ֤ÐèÒª²åÈëеÄÏî¡£Óï·¨ÈçÏ£º

array.splice(start, deleteCount, ...items)

µÇ¼ºó¸´ÖÆ

ÆäÖУº

start£ºÒª×îÏÈɾ³ýµÄÏîµÄË÷Òý

deleteCount£ºÒªÉ¾³ýµÄÏîµÄÊýÄ¿

items£º¿ÉÑ¡²ÎÊý £¬Òª²åÈëµ½×îÏÈλÖúóµÄÐÂÏî

ʾÀý£º

const arr = [1, 2, 3, 4, 5];
arr.splice(2, 1); // ɾ³ýµÚ3ÏË÷ÒýΪ2£©
console.log(arr); // Êä³ö£º[1, 2, 4, 5]

µÇ¼ºó¸´ÖÆ

ÒªÁì¶þ£ºÊ¹ÓÃfilter()ÒªÁì

filter()ÒªÁ콨ÉèÒ»¸öеÄÊý×é £¬ÆäÖÐÖ»°üÀ¨Í¨¹ý¸ø¶¨²âÊÔÌõ¼þµÄÏî¡£ÎÒÃÇ¿ÉÒÔʹÓÃËüÀ´ÌÞ³ý²»ÐèÒªµÄÏî¡£Óï·¨ÈçÏ£º

array.filter(callback(currentValue, index, array))

µÇ¼ºó¸´ÖÆ

ÆäÖУº

callback£ºÒ»¸öº¯Êý £¬Ëü·µ»ØÒ»¸ö²¼¶ûÖµ £¬Ö¸Ê¾Òª»ò²»Òª±£´æ¸ÃÏî

ʾÀý£º

const arr = [1, 2, 3, 4, 5];
const newArr = arr.filter((item) => item !== 3); // ɨ³ýֵΪ3µÄÏî
console.log(newArr); // Êä³ö£º[1, 2, 4, 5]

µÇ¼ºó¸´ÖÆ

ÒªÁìÈý£ºÊ¹ÓÃslice()ºÍconcat()ÒªÁì

slice()ÒªÁ콨ÉèÒ»¸öÐÂÊý×é £¬ÆäÖаüÀ¨Ô­Ê¼Êý×éÖÐÖ¸¶¨¹æÄ£ÄÚµÄÏî¡£ÎÒÃÇ¿ÉÒÔ½«ÆäÓëconcat()ÒªÁìÍŽáʹÓà £¬½«ËùÐèµÄÏÖƵ½ÐÂÊý×éÖС£Óï·¨ÈçÏ£º

const newArr = arr.slice(0, start).concat(arr.slice(end + 1));

µÇ¼ºó¸´ÖÆ

ÆäÖУº

start£ºÒª×îÏȸ´ÖƵÄË÷Òý

end£ºÒª×èÖ¹¸´ÖƵÄË÷Òý

ʾÀý£º

const arr = [1, 2, 3, 4, 5];
const newArr = arr.slice(0, 2).concat(arr.slice(3)); // ɾ³ýµÚ3ÏË÷ÒýΪ2£©
console.log(newArr); // Êä³ö£º[1, 2, 4, 5]

µÇ¼ºó¸´ÖÆ

ÒÔÉϾÍÊÇjsÔõôɾ³ýÊý×éÖеÄijһÏîµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ