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
3 Answers
Reset to default 6The 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);
cand
is the full array and has no propertyalter
. The objects within the array do – charlietfl Commented Oct 14, 2017 at 14:46