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"] },

                ]

var resultSet = $.grep(courses, function (element, index) {
    // return allo element with authorize === Administrator;
});

console.log(JSON.stringify(resultSet))

    
// 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 = [];
    for(var i=0; i<courses.length; i++){
        for(var j=0; courses[i]&&courses[i].authorize&&          j<courses[i].authorize.length;j++){
            for(var k=0; k<authTypes.length; k++){
                if(courses[i].authorize[j] === authTypes[k])
                    results.push(courses[i]);
            }
        }
    }
    return results;
})(acceptedTypes);
console.log(JSON.stringify(resultSetB));