JSFiddle - React, Tailwind, and code Playground
by kougiland
JavaScript
var otherStuff = {};
courses = [
{ index: ['', 'overview'], moduleId: 'overview', title: 'overview', nav: 1 , authorize: ["sellers", "taxi"]},
{ index: 'rating', moduleId: 'rating', title: 'rating', nav: 3},
{ index: 'example', moduleId: 'example', title: 'example', nav: 5, authorize: ["Visitor", "Administrator"] },
{ index: 'apple', moduleId: 'apple', title: 'apple', nav: 6, authorize: ["User", "Administrator"] },
];
// EnjoysTurtles/Andrew
var acceptedTypes = ["Administrator", "Visitor"];
// ECMA 5
var resultSetA = (function(authTypes){
return courses.filter(function(crs){
return crs && crs.authorize
&& crs.authorize.indexOf && authTypes.some(function(authType){
return ~crs.authorize.indexOf(authType);
});
});
})(acceptedTypes);
console.log(JSON.stringify(resultSetA));
//Compatibility with all browsers (ECMAScript 3):
var resultSetB = (function(authTypes){
var results = [], i, j, k, works;
for(i=0; i<courses.length; i++){
if(courses[i]&&courses[i].authorize&&courses[i].authorize.length){
works = false;
for(j=0; j<courses[i].authorize.length; j++){
for(k=0; k<authTypes.length; k++){
works = works || courses[i].authorize[j] === authTypes[k];
}
}
if(works)
results.push(courses[i]);
}
}
return results;
})(acceptedTypes);
console.log(JSON.stringify(resultSetB));