Why is the below returning undefined? Shouldn't I just get back [{name:'bug']?
var a= [{name:'dark'},{name:'bug'}]
a.map(function (obj) {
if (obj.name !== 'dark'){
return obj
}
})
//returns [undefined,{name:'bug}]
.map means you are mapping something for every item in an array. If you do not return anything, it will return undefined
If you wish to get certain values based on condition, you should use .filter
var a = [{
name: 'dark'
}, {
name: 'bug'
}]
var b = a.filter(function(obj) {
return obj.name !== 'dark'
})
console.log(b)
Note: Most of array functions have compatibility issues and you should check it before using it.
Array.filterfilter does.
mapwithfilter.