filter index javascript code example
Example 1: .filter js
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
console.log(result);
Example 2: javascript object filter
function objectFilter = (obj, predicate) =>
Object.keys(obj)
.filter( key => predicate(obj[key]) )
.reduce( (res, key) => (res[key] = obj[key], res), {} );
var scores = {
John: 2, Sarah: 3, Janet: 1
};
var filtered = objectFilter(scores, num => num > 1);
console.log(filtered);
Example 3: javascript array filter
var numbers = [1, 3, 6, 8, 11];
var lucky = numbers.filter(function(number) {
return number > 7;
});
Example 4: array.filter in javascript
const array = [2, 3, 4]
if (event.target.checked) {
newValue.push(option);
} else {
newValue = newValue.filter(item => item.id != option.id);
}
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter(word => word.length > 6);
Example 5: js filter items by index
let newArray = arr.filter(callback(element[, index, [array]])[, thisArg])