Array.prototype.filter returns element that passes provided predicate, so in you case that will be item for which item is true. To achieve what you want, you need to filter items first and the extract second element using map:
var array = [[8, 'first'], [8, 'second'], [1, 'empty'], [8, 'third'], [9, 'empty']];
var result = array.filter(function(item) {
return item[0] == 8;
}).map(function (item) {
return item[1];
});
console.log(result);
Or use reduce:
var array = [[8, 'first'], [8, 'second'], [1, 'empty'], [8, 'third'], [9, 'empty']];
var result = array.reduce(function(result, item) {
return item[0] == 8 ? result.concat(item[1]) : result;
}, []);
console.log(result);
If you want to use features of ES6 it can get event shorter:
const array = [[8, 'first'], [8, 'second'], [1, 'empty'], [8, 'third'], [9, 'empty']];
const result1 = array.filter(([item]) => (item == 8)).map(([,item]) => item);
const result2 = array.reduce((res, [n, str]) => n == 8 ? [...res, str] : res, []);
console.log(result1);
console.log(result2);
EDIT: To understand why your function is not working you need to understand how Array.prototype.filter() works. It creates a new array made of elements for which provided predicate function (i.e. checking some condition and returning Boolean) returns true. For all sub-arrays that have 8 as first item, second element will decide if that element will be used for creating new array.
So for each element of array (first [8, 'first'], next [8, 'second'] etc.) your function will be evaluated, and element for which true is returned, will be added to new array:
[[8, 'first'], [8, 'second'], [1, 'empty'], [8, 'third'], [9, 'empty']]
true, true, false, true, false
=> [[8, 'first'], [1, 'empty'], [8, 'third']]
filterdoesn'tmap- I think you wantmap(and filter to filter out the unwanted results)filter. Based on the return of your filtering function, it will keep or not the current element. It won't modify it or use the object you return instead.