0

i am trying to return all elements matching authorize === Administrator or authorize === Visitor without success

hier ist what i have sofar. Jsfiddle

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 all element with authorize === Administrator or authorize === Visitor;
});

console.log(JSON.stringify(resultSet))

can anyone help me on that ?

Gildas.Tambo
  • 22,173
  • 7
  • 50
  • 78

2 Answers2

1

use $.inArray() to check whether the value exists in the array. Array.indexOf() was not used because of IE compatability

var resultSet = $.grep(courses, function (element, index) {
    return $.inArray('Administrator', element.authorize) != -1
});

Demo: Fiddle


But since you are using jQuery 2+, I assume you are not worried about ID < 9 then

var resultSet = $.grep(courses, function (element, index) {
    return element.authorize && element.authorize.indexOf('Administrator') != -1
});

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

Both of my solutions prevent any variables from leaking into your closure and require no library. The self executing functions are so you can replace the literal array ["Administrator", "Visitor"] with any type of authorization.

The ECMA 5 one is also a good example of functional programming. The accepted answer DOES NOT WORK because it lacks the ability to test against multiple authtypes.

JSFIDDLE: http://jsfiddle.net/FcFDG/3/

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));
Andrew Templeton
  • 1,656
  • 10
  • 13
  • somehow i have 2 element with example see this please[{"index":"example","moduleId":"example","title":"example","nav":5,"authorize":["Visitor","Administrator"]},{"index":"example","moduleId":"example","title":"example","nav":5,"authorize":["Visitor","Administrator"]},{"index":"apple","moduleId":"apple","title":"apple","nav":6,"authorize":["User","Administrator"]}] – Gildas.Tambo Feb 27 '14 at 03:56
  • didn't see sparse data, one sec for ECMA3 – Andrew Templeton Feb 27 '14 at 04:04
  • Thought you wanted to doubles for dual-authorized users - my bad – Andrew Templeton Feb 27 '14 at 04:13
  • the jsfiddle you just posted doesn't work and why are you using filter in // ECMA 5 filter 5 ist just for html for array you should use grep see the diff between filter and grep please – Gildas.Tambo Feb 27 '14 at 04:19
  • Filter is an Array.prototype member. Stack overflow is a learning platform so I thought I would should you how to do this with raw JS... I also would like to point out the $.grep code is still wrong in the answer. It's working when I run it and when I run it locally... – Andrew Templeton Feb 27 '14 at 04:25
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter This is a native function and NOT the $.filter call. The native Array.prototype.filter is much faster than the $.filter, and requires no libraries. I think you misunderstand, Array.prototype.filter !== $.filter... Note that there is not a single $ in my code. – Andrew Templeton Feb 27 '14 at 04:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48547/discussion-between-kougiland-and-enjoys-turtles) – Gildas.Tambo Feb 27 '14 at 08:09