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
Return valueIf no elements pass the test, an empty array will be returned.
filter() returns empty array. Save this question.
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 .
Because Array is type of object , the fact that an empty Array is conversed to true is correct.
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);
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)}`);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With