0

Trying to make a function that removes only the "strings" from the array. I'm looking to only have the numbers left. I've already accomplished this by adding numbers only to a newArray, and I was looking into the splice method but could not figure out how to write it. As you see in code delete works, but returns undefined in its spot.

function numbersOnly(arr){

    for(var i = 0; i < arr.length; i++){
        if(typeof arr[i] === "string"){
        delete arr[i];
        }
    }
    console.log(arr);
 }
numbersOnly([1, "apple", -3, "orange", 0.5, 22, "hello", 6])

returns [1, undefined, -3, undefined, 0.5, 22, undefined, 6]

Robert Amato
  • 59
  • 1
  • 2
  • 6

6 Answers6

1

var arr = [1, "apple", -3, "orange", 0.5, 22, "hello", 6];
var out = arr.filter(function(f) { return typeof(f) !== 'string';});
console.log( out );
Jarek Kulikowski
  • 1,399
  • 8
  • 9
0

You would want to use array.splice() instead of delete, which is for properties.

0

I would have a go another way around

function numbersOnly(arr){
let numbers = [];
for(var i = 0; i < arr.length; i++){
    if(typeof arr[i] === "number"){
    numbers.push(arr[i]);
    }
}
console.log(numbers);

}

Jake11
  • 801
  • 2
  • 11
  • 24
0

If you delete sth in an array, there stays an empty space which is undefined. You need to shift the array, so replace

 delete arr[i];

with

arr=arr.slice(0,i).concat(arr.slice(i+1));
i--;

or using splice:

arr.splice(i,1);
i--;//dont jump over this index

or using a for loop:

for(var b=i;b<arr.length-1;b++){
  arr[b]=arr[b+1];
}
arr.length-=1;
i--;

Alternatively you could filter:

arr.filter(el=>typeof el==="number");//numbers only
//or
arr.filter(el=>typeof el!=="string");//not stringd
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

If the array's cell contains a number and that number yields true for if Number(). Then that value will be added to a secondary array and returned.

  function numbersOnly(arr){ 

      var numbersarr = [];
      var i;
      var k = 0;
      for (i=0; i< arr.length ;i++) {
    
      if (  Number(arr[i])   ) {
           
            numbersarr[k++] = arr[i];
         }
     }
      
      console.log(numbersarr);
      return numbersarr;
     }

    var strarr = [1, "apple", -3, "orange", 0.5,
                  22, "hello", 6 , "car", "123" , 
                  "zoo", "456"];
        
    numbersOnly(strarr);    
    numbersOnly(["foo","bar", "something"]);
    

And this is how your output will be:

  [1, -3, 0.5, 22, 6, "123", "456"]
Mohammed Joraid
  • 6,202
  • 2
  • 27
  • 38
  • Did you even try this? It should remove all elements (they're all strings) but it doesn't. Also try with an input of `[ "foo", "bar", "baz" ]`. – melpomene Jul 19 '17 at 19:35
  • Yes, in fact, I tried it. I ran the code first, then I brought it here. I updated my answer to show the output. – Mohammed Joraid Jul 19 '17 at 19:38
  • Your code left `"123"` and `"456"` in the array even though they're strings. This is wrong. (Also, you should really try running it on `[ "foo", "bar", "baz" ]`.) – melpomene Jul 19 '17 at 19:40
  • @melpomene He did not specify in his requirement that "123" is not considered a number. and 123 a number. – Mohammed Joraid Jul 19 '17 at 19:41
  • Javascript itself specifies that `"123"` is a string (and `123` is a number). But even if you ignore that, `numbersOnly([ "foo", "bar", "baz" ])` outputs `[ "bar" ]`. Surely you don't think that `"bar"` is considered a number? – melpomene Jul 19 '17 at 19:43
  • @melpomene look, I hear you, I know what you mean. But he wanted to remove the cells with alphabets in them. – Mohammed Joraid Jul 19 '17 at 19:46
  • `numbersOnly(["foo", "bar", "baz"])` outputs `["bar"]`. It should output `[]`. Your code is broken. – melpomene Jul 19 '17 at 19:48
  • @melpomene You are correct. that got taken care of. – Mohammed Joraid Jul 19 '17 at 20:04
  • @melpomene on a side note. Number(arr[i]) returns "123" as a number. Is that wrong? – Mohammed Joraid Jul 19 '17 at 20:13
0

use .splice(index, count)

function numbersOnly(arr) {
  for(var i = 0; i < arr.length; i++) {
    if(typeof arr[i] === "string") {
      arr.splice(i, 1);
    }
  }
  console.log(arr);
}
numbersOnly([1, "apple", -3, "orange", 0.5, 22, "hello", 6]);
ewwink
  • 18,382
  • 2
  • 44
  • 54