Cov txheej txheem:

Txoj kev twg tshem tawm qhov kawg ntawm qhov kawg ntawm ib qho array?
Txoj kev twg tshem tawm qhov kawg ntawm qhov kawg ntawm ib qho array?

Video: Txoj kev twg tshem tawm qhov kawg ntawm qhov kawg ntawm ib qho array?

Video: Txoj kev twg tshem tawm qhov kawg ntawm qhov kawg ntawm ib qho array?
Video: Saib Mis paub Poj niam (hluas nkauj) tus yam ntxwv lub siab 2024, Kaum ib hlis
Anonim

Pope () txoj kev tshem tawm cov ntsiab lus kawg ntawm ib array , thiab rov qab los keeb . Nco tseg: Qhov no txoj kev hloov qhov ntev ntawm ib array . Tswv yim: To tshem tawm thawj keeb ntawm ib array , siv lub zog () txoj kev.

Tsis tas li ntawd, koj yuav tshem tawm qhov kawg ntawm ib qho array li cas?

Rau tshem tawm tus kawg n cov ntsiab lus los ntawm ib array , siv arr. splice(-n) (nco lub "p" hauv "splice"). Tus nqi rov qab yuav yog qhov tshiab array muaj cov khoom tshem tawm. Txoj kev no yuav pab tau ntau dua rho tawm thiab khaws cia xeem ntawm ib array.

Ib yam li ntawd, uas tsim nyob rau hauv txoj kev tshem tawm lub xeem dhau los ntawm ib qho array thiab xa rov qab lub caij ntawd? Lub pop txoj kev tshem tawm lub xeem dhau los ntawm ib qho array thiab xa rov qab tus nqi ntawd rau tus hu. pop yog txhob txwm generic; qhov no txoj kev tuaj yeem hu los yog siv rau cov khoom zoo li arrays.

Kuj paub yog, koj ua li cas thiaj nrhiav tau qhov kawg ntawm ib qho array?

JavaScript: Tau tus xeem ntawm ib array Sau JavaScript ua haujlwm rau tau tus xeem ntawm ib array . Dhau ib qho parameter 'n' yuav rov qab los kawg 'n' cov ntsiab lus ntawm array . ES6 Version: var kawg = function kawg ( array , n) { yog ( array == null) rov void 0; if (n == null) rov array [ array.

Yuav ua li cas koj tshem tawm ib yam khoom los ntawm ib qho array?

Muaj ntau txoj hauv kev thiab cov tswv yim uas koj tuaj yeem siv los tshem tawm cov ntsiab lus ntawm JavaScript arrays:

  1. pop - Tshem tawm ntawm qhov kawg ntawm ib qho Array.
  2. Hloov - Tshem tawm ntawm qhov pib ntawm Array.
  3. splice - tshem tawm ntawm qhov tshwj xeeb Array Performance index.
  4. lim - tso cai rau koj kom tshem tawm cov ntsiab lus los ntawm Array.

Pom zoo: