Dažādi veidi, kā izdzēst vienumu no masīva, izmantojot JavaScript

Dažādi veidi, kā izdzēst vienumu no masīva, izmantojot JavaScript

Programmā Javascript mums nav nevienas metodes array.remove() elementa dzēšanai. mums būs masīvs, un mums ir jāizdzēš dotais vienums no šī masīva un jāatgriež iegūtais masīvs konsolē.

Atšķirības veidi, kā izdzēst vienumu no masīva

Šīs ir šādas problēmas risināšanas metodes:

Satura rādītājs

Piezīme: Ir dažas citas metodes, kuras izveido ar JavaScript iebūvētām metodēm.

1. metode: Izmantojot cilpai un spiediet () Metode

Šī metode nemainīs sākotnējo masīvu. Pirmkārt, jums ir jāizveido tukšs () masīvs un pēc tam jāpāriet uz jauno masīvu un jānospiež tikai tie elementi, kurus vēlaties.

Piemērs: Šis piemērs parāda iepriekš izskaidroto pieeju.

Javascript




let arr = [> 'gfg'> ,> 'GFG'> ,> 'g'> ,> 'techcodeview.com'> ];> const arrayWithoutGFG = [];> for> (let i = 0; i if (arr[i] !== 'GFG') { arrayWithoutGFG.push(arr[i]); } } // arr is same console.log(arr); console.log(arrayWithoutGFG);>

Izvade

[ 'gfg', 'GFG', 'g', 'techcodeview.com' ] [ 'gfg', 'g', 'techcodeview.com' ] 

2. metode: izmantošana Pop() metode

Šo metodi izmanto, lai izdzēstu pēdējo masīva elementu un atgrieztu izdzēsto vienumu kā izvadi. Elementa noņemšana samazina masīva garumu.

Piemērs: Šajā piemērā metode pop() tiek izmantota masīva elementa dzēšanai.

Javascript




function> myFunc() {> > let arr = [> 'gfg'> ,> 'GFG'> ,> 'g'> ,> 'techcodeview.com'> ];> > let name = arr.pop();> > console.log(name);> > console.log(arr.length)> }> myFunc();>

Izvade

techcodeview.com 3 

3. metode: izmantošana Shift() metode

Šo metodi izmanto, lai izdzēstu elementu no masīva sākuma. Šo metodi izmanto, lai atgrieztu pirmo masīva elementu. Tas arī samazina sākotnējā masīva garumu.

Piemērs: Šajā piemērā metode shift() tiek izmantota, lai dzēstu pirmo masīva elementu.

Javascript




function> myFunc() {> > let arr = [> 'gfg'> ,> 'GFG'> ,> 'g'> ,> 'techcodeview.com'> ];> > let name = arr.shift();> > console.log(name);> > console.log(arr.length)> }> myFunc();>

Izvade

gfg 3 

4. metode: izmantošana splice() metode

Šo metodi izmanto esošā elementa dzēšanai vai masīva satura aizstāšanai, noņemot/pievienojot jaunu elementu.

Piemērs: Šajā piemērā savienojuma metode tiks izmantota, lai dzēstu vienumu no masīva.

Javascript




function> myFunc() {> > let myFruit = [> 'apple'> ,> 'banana'> ,> 'grapes'> ,> 'strawberry'> ];> > const removed = myFruit.splice(2, 2,> 'guava'> );> > > // Removed element in the array> > console.log(removed);> > // Length of the original array after deleting> > console.log(myFruit.length);> > // Original array after deleting the array> > console.log(myFruit);> }> myFunc();>

Izvade

[ 'grapes', 'strawberry' ] 3 [ 'apple', 'banana', 'guava' ] 

5. metode: lietošana filtra() metode

Šī metode atgriež jauno masīvu. Tie masīva elementi, kas atbilst funkcijas nosacījumiem, tiek nodoti tikai jaunajam masīvam. Šī metode nemaina sākotnējo masīvu.

Piemērs: Šajā piemērā mēs izmantosim metodi filter(), lai izdzēstu vienumu no masīva.

Javascript




const arr = [2, 7, 9, 15, 19];> function> isPrime(n) {> > for> (let i = 2; n>i; i++) {> > if> (n % i === 0) {> > return> false> ;> > }> > }> > return> n>1;> }> console.log(arr.filter(isPrime));>

Izvade

[ 2, 7, 19 ] 

6. metode: lietošana dzēst operatoru

Šis operators ir precīzāk izmantots JavaScript objekta rekvizītu dzēšanai.

Piemērs: Šajā piemērā mēs izmantosim JavaScript dzēšanas operatoru, lai dzēstu vienumus no masīva.

Javascript




const arr = [2, 7, 9, 15, 19];> delete> arr[3];> console.log(arr);>

Izvade

[ 2, 7, 9, , 19 ] 

7. metode: izmantošana Lodash _.remove() Metode

The _.remove() metode tiek izmantots, lai noņemtu visus elementus no masīva, kas predikāts atgriež True un atgriež noņemtos elementus.

Piemērs: Šis piemērs parāda iepriekš izskaidroto pieeju.

Javascript




const _ = require(> 'lodash'> );> let arr = [1, 2, 3, 4, 5];> let even = _.remove(arr,> function> (n) {> > return> n % 2 == 0;> });> console.log(> 'Original Array '> , arr);> console.log(> 'Removed element array '> , even);>

Izvade:

Original Array [ 1, 3, 5 ] Removed element array [ 2, 4 ]