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

javascript - filter() returns empty array - Stack Overflow

programmeradmin4浏览0评论

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;

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

Share Improve this question asked Oct 14, 2017 at 14:33 HJWHJW 1,0322 gold badges14 silver badges38 bronze badges 1
  • 3 cand is the full array and has no property alter. The objects within the array do – charlietfl Commented Oct 14, 2017 at 14:46
Add a comment  | 

3 Answers 3

Reset to default 6

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)}`);

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);

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);

发布评论

评论列表(0)

  1. 暂无评论