Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filter() returns empty array

Next noob question in the seemingly endless array that is learning a new language. Yes, I do feel like I know nothing. Yes, I know this is mega easy. No, i cannot figure it out by myself.

I have an array that I am trying to access via filter. No particular reason, just a challenge on one of the free coding sites:

var cand = [
  {
    name: 'Kevin',
    alter: 19,
  },
  {
    name: 'Walter',
    alter: 22,
  },
  {
    name: 'Herbert',
    alter: 28,
  },
  {
    name: 'Kristin',
    alter: 31,
  },
  {
    name: 'Obergine',
    alter: 39,
  },
  {
    name: 'Hailey',
    alter: 44,
  }
];

var alter = function(){
  return cand.alter < 30;
}

var filter = cand.filter(alter);

filter;

This returns an empty array. I have a feeling that I am not accessing each alter property correctly. I also have a feeling that I need a loop that cycles through each of the person's properties. Please help, thank you

like image 968
HJW Avatar asked Oct 14 '17 14:10

HJW


People also ask

Does filter return an empty array?

Return valueIf no elements pass the test, an empty array will be returned.

What does filter return if nothing is found?

filter() returns empty array. Save this question.

Why does filter return undefined?

The filter functions returns an empty array if nothing is found, which is a truth-y value, so the ternary condition never triggers the false condition. Thus, the first entry in an empty array is undefined .

Does empty array return true?

Because Array is type of object , the fact that an empty Array is conversed to true is correct.


3 Answers

Set an argument to the filter function and use that in the return.

var cand = [{name: 'Kevin',alter: 19,},{name: 'Walter',alter: 22,},{name: 'Herbert',alter: 28,},{    name: 'Kristin',alter: 31,},{name: 'Obergine',alter: 39,},{name: 'Hailey',alter: 44,}];

// add argument to the filter function | element
var alter = function(element) {
  return element.alter < 30; //use the argument here.
}

var filter = cand.filter(alter);

console.log(filter);
like image 131
Mouser Avatar answered Oct 16 '22 16:10

Mouser


You need to add the parameter for the callback.

var alter = function(cand) {
//                   ^^^^
    return cand.alter < 30;
}

var cand = [{ name: 'Kevin', alter: 19 }, { name: 'Walter', alter: 22 }, { name: 'Herbert', alter: 28 }, { name: 'Kristin', alter: 31 }, { name: 'Obergine', alter: 39 }, { name: 'Hailey', alter: 44 }];

var alter = function(cand){
    return cand.alter < 30;
}

var filter = cand.filter(alter);

console.log(filter);
like image 22
Nina Scholz Avatar answered Oct 16 '22 14:10

Nina Scholz


The filter() method creates a new array with all elements that pass the test implemented by the provided function.

You need to pass an parameter as filter function required. You can use also ES6 filter with Arrow function.

An arrow function expression has a shorter syntax than a function expression and does not bind its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.

var cand = [{name: 'Kevin',alter: 19},{name: 'Walter',alter: 22},{name: 'Herbert',alter: 28},{name: 'Kristin',alter: 31},{name: 'Obergine',alter: 39},{name: 'Hailey',alter: 44}],
  alter = function(item){
    return item.alter < 30;
  },
  filter = cand.filter(alter);
//using normal javascript
console.log('Result Using normal filter javascript :-'+JSON.stringify(filter));

console.log('*************************');
//You can also you ES6 with arrow function

let filter1 = cand.filter(obj=> {return obj.alter<30});

console.log(`Result Using arrow function :- ${JSON.stringify(filter1)}`);
like image 29
Narendra Jadhav Avatar answered Oct 16 '22 15:10

Narendra Jadhav