最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - I get [1,15], instead of [1,0,15]. JavaScript filter - Stack Overflow

programmeradmin3浏览0评论

Create a function that takes a list of non-negative integers and strings and returns a new list with the strings filtered out. When I pass [1,'a','b',0,15], it returns [1,15] instead of [1,0,15]. Please suggest any corrections.

function filter_list(l) {
  // Return a new array with the strings filtered out
  var filt = l.filter(function(x) {
    if (typeof(x) === 'number')
      return x;
  });
  return filt;
}

Create a function that takes a list of non-negative integers and strings and returns a new list with the strings filtered out. When I pass [1,'a','b',0,15], it returns [1,15] instead of [1,0,15]. Please suggest any corrections.

function filter_list(l) {
  // Return a new array with the strings filtered out
  var filt = l.filter(function(x) {
    if (typeof(x) === 'number')
      return x;
  });
  return filt;
}

Share Improve this question edited Jul 9, 2018 at 7:43 Priya 3343 silver badges8 bronze badges asked Jul 9, 2018 at 5:17 Nikita Nikita 2115 silver badges16 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 4

0 is falsey, and if the value returned by the filter callback is falsey, the item being iterated over will not be included in the final array. Simply return typeof x === 'number' instead:

function filter_list(l) {
  return l.filter(x => typeof x === 'number');
}
console.log(filter_list([1, 'a', 'b', 0, 15]));

You are returning x instead of the result of the boolean typeof(x) === 'number'. x is 0 which is falsy so it gets filtered out.

There are several falsy values in javascript. For instance this filter returns an empty array:

console.log([0, '', 'A'/2, null, undefined].filter(x => x))
  

Instead, return the result of the test directly from the filter callback:

function filter_list(l) {
    // Return a new array with the strings filtered out
    return l.filter(x => typeof x === 'number')
   
  }
  console.log(filter_list([1, 'a', 'b', 0, 15]));
  

0 is considered as false. return true or false from filter function as below.

function filter_list(l) {
  // Return a new array with the strings filtered out
  return l.filter(function(x) {
    if (typeof(x) === 'number')
      return true;
  });
}
console.log(filter_list([1, 'a', 'b', 0, 15]));

A filter function accepts the boolean value to return.

Now take look for your code.

if (typeof(x) === 'number')
  return x;

As per your code return x will convert into return 0 and finally filter will understand returned value as false. Because 0 bees false.

So, you can use arrow function:

function filter_list(l) {
    return l.filter(x => typeof x === 'number');
}

Or,

function filter_list(l) {
    return l.filter(function(x) {
        if (typeof(x) === 'number')
        return true;
    });
}
        package ArrayPractice;
        import java.util.*;
        public class Question8 {
            public static ArrayList<Integer> dataguise= new ArrayList<>();
            public void filterArray(Object[] data){
                for(int i=0;i<data.length;i++){
                    if(data[i] instanceof Integer){
                        dataguise.add(i);
                    }}
            }
            public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("Enter the array range: ");
                int i= sc.nextInt();
                int [] data= new int[i];
                Object [] demo= new Object[i];
                for (int y = 0; y < data.length; y++) {
                    System.out.println("Enter the Array number: " + y);
                    if(sc.hasNextInt()){
                        int value= sc.nextInt();
                        dataguise.add(value);
                    }else{
                        demo[y]= sc.next();
                    }
        
                }
                Question8 question8= new Question8();
                question8.filterArray(demo);
        
                for(int v: dataguise){
                    System.out.println(v);
                }
        
            }
        
        }

This is a short way to write the it.

function filter_list(l) 
{ 
    return l.filter(Number.isInteger);
}
发布评论

评论列表(0)

  1. 暂无评论