Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
15
rated 0 times [  19] [ 4]  / answers: 1 / hits: 32025  / 8 Years ago, mon, october 10, 2016, 12:00:00

I am trying to output the first two objects in the events array using indexOf.



This doesn't return anything:



var whiteList=['css','js'];

var events =[
{file: 'css/style.css', type:'css'},
{file: 'js/app.js', type:'js'},
{file: 'index/html.html', type:'html'}
];

var fileList= events
.filter(function(event){
return event.type.indexOf(whiteList) >- 1
})

console.log(fileList);


If I change the function like this, it returns the css and js object, although I expected it to return the html object.



var fileList= events
.filter(function(event){
return event.type.indexOf('html')
})

More From » javascript

 Answers
104

You are doing it wrong, it should go like this.





var whiteList = ['css', 'js'];

var events = [{
file: 'css/style.css',
type: 'css'
}, {
file: 'js/app.js',
type: 'js'
}, {
file: 'index/html.html',
type: 'html'
}];

var fileList = events.filter(function(event) {
return whiteList.indexOf(event.type) > -1
})

console.log(fileList)




[#60444] Friday, October 7, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andrewb

Total Points: 259
Total Questions: 124
Total Answers: 90

Location: Ivory Coast
Member since Sun, Mar 7, 2021
3 Years ago
;