-3

I want to remove the negative elements of the copied array by pop() method but it removes only the last element of the array. Anyone knows where's the problem? The code is in the imageenter image description here

I tried:

const scores = [128, 0, -8, 50, 2, 3];
const betterScores = Array.from(scores);

for (const s of betterScores) {
  if (s < 0) {
    betterScores.pop(s);
  }
}

console.log(betterScores);

I wanted to have betterScores = [128,50,2,3] but I got [128,0,-8,50,2].

Andy
  • 61,948
  • 13
  • 68
  • 95
pegah
  • 5
  • 3
  • 3
    Have you read the [documentation of `Array.pop()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop)? It does not accept arguments (it ignores them, in fact) and always removes the last element of the array. Use [`Array.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) to produce the array you need. – axiac Apr 07 '23 at 11:06
  • [javascript - remove array element on condition](https://stackoverflow.com/q/15995963) – VLAZ Apr 07 '23 at 11:38
  • Does this answer your question? [How can I remove a specific item from an array in JavaScript?](https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array-in-javascript) See also: [Filter array of object with conditional statement](https://stackoverflow.com/questions/67590053/filter-array-of-object-with-conditional-statement) and many others. – Abdul Aziz Barkat Apr 07 '23 at 11:55

6 Answers6

2

Use filter instead of for loop:

const betterScores = scores.filter(s => s > 0);
Aksen P
  • 4,564
  • 3
  • 14
  • 27
  • 1
    This answer is technically correct but it does not help the OP improve their knowledge. Add a link to the documentation of `Array.filter()` to improve it. You can also add a link to the documentation of `Array.pop()` to help the OP understand why their approach does not work. – axiac Apr 07 '23 at 11:09
  • @axiac, I agree. But this aspect also depends on the personal eagerness to learn more. – Aksen P Apr 07 '23 at 11:39
1

You can do easily by filter in javascript.

Try this:

Filter method doc: Filter Method

const scores = [128, 0, -8, 50, 2, 3];
const betterScores = Array.from(scores);

const result = betterScores.filter(item => item > 0);

console.log(result);
Priyen Mehta
  • 1,096
  • 1
  • 5
  • 19
  • 1
    This answer is technically correct but it does not help the OP improve their knowledge. Add a link to the documentation of `Array.filter()` to improve it. You can also add a link to the documentation of `Array.pop()` to help the OP understand why their approach does not work. – axiac Apr 07 '23 at 11:09
  • So, Which method do you want? – Priyen Mehta Apr 07 '23 at 12:00
  • @PriyenMehta, The splice solution looks good. – pegah Apr 07 '23 at 12:02
1

pop method does not expect any arguments, it automatically removes last element of array. To remove element at an index you can use splice method.

const arrayOfNumbers = [1, 2, 3, 4];

arrayOfNumbers.splice(1, 1);

console.log(arrayOfNumbers); // [1, 3, 4]

So to remove one element at an index you would use splice(n, 1), where n is index if element you are removing and 1 is count of elements(for your case you would default 2nd argument to 1 and just give right index. Also to have access to index for of loop wont work. Use basic for loop or forEach to have access to index

Edward
  • 189
  • 2
  • 7
1

filter is the best approach to use to solve this problem as the other users have commented.

To answer the specific question about why your code doesn't work:

The pop() method removes the last element from an array and returns that element. This method changes the length of the array. (my emphasis)

If you really wanted to use a loop to remove elements you might use a simple for loop and splice elements from a particular index. But since the length of the array gets shorter when you remove elements it's usually best to start from the end of the array, and iterate toward the start.

const scores = [128, 0, -8, 50, 2, 3];

for (let i = scores.length - 1; i >= 0; --i) {
  if (scores[i] <= 0) {
    scores.splice(i, 1);
  }
}

console.log(scores);
Andy
  • 61,948
  • 13
  • 68
  • 95
0

I'm sure you are not aware of pop method of array, It pop last element of you array. If you task is to only get positive number then using filter would be better solution

const scores = [128, 0, -8, 50, 2, 3];
const result = scores.filter(item => item >= 0);

console.log(result);

but if you task is to do using some method to manually remove element form array you can use splice method something like this

const scores = [128,0,-8,50,2,3];
const betterScores = Array.from(scores);

for(const s in betterScores){
    if(betterScores[s] < 0){
  betterScores.splice(s,1);
    }
}
console.log(betterScores);
hackCharms
  • 46
  • 5
0

you may try this code:

const scores = [128, 0, -8, 50, 2, 3];
const betterScores = Array.from(scores);

for (const s of betterScores) {
  if (s < 0) {
    betterScores.splice(betterScores.indexOf(s), 1)
  }
}
Namatullah Shahbazi
  • 236
  • 1
  • 8
  • 21