-1

I am working on an angularJS widget (my first) and I currently am looking for an angularJS solution to my problem.

Basically I have one array containing a list of string values.

var array1 = [
    "Need to Know",
    "Test Category 2",
    "News"
];

and another array containing another list of string values

var array2 = [
    "need to know",
    "release notes",
    "NEWS"
];

I need a true statement if any element from one array matches any element from the other array. The result also needs to be case insensitive.

Here is my current solution and works great.

angular.module("myWidget", function(...){
    // angular code here
})
.service('arrayService', function() {
    function arrayToLowerCase(array) {
        return array.join("~!~").toLowerCase().split("~!~");
    }

    function arrayElementIsInArray(array1, array2) {
        for (var i in array1) {
            if (array2.indexOf(array1[i]) >= 0) {
                return true;
            }
        }
        return false;
    }

    function arrayCompare(array1, array2) {
        return arrayElementIsInArray(arrayToLowerCase(array1), arrayToLowerCase(array2));
    }

    return {
        arrayToLowerCase: arrayToLowerCase,
        arrayElementIsInArray: arrayElementIsInArray,
        arrayCompare: arrayCompare
    };

})

the problem is my javascript coders (I primary work in c#) feel there is a more angularJS way to do this but they have brought nothing to the table as a definitive solution. It was suggested that the $filter module might be useful but I didn't see how it would exactly solve my problem.

If I already have the best solution, then awesome. If not please let me know what you think and lets go from there.

Thanks in advance.

EDIT: In response to some of the answers, I felt that I might have misinterpreted my request. What I am asking is there a built in function that angular provides that does this out of the box?

After researching this a bit more; the $filter Module will probably do it with a custom comparater implemented but that seems like way overkill for what I am looking for.

The current responses are all good stuff though. Thanks again!

Kenneth Garza
  • 1,886
  • 14
  • 12
  • 2
    Why are you lowercasing like that? You have `map` like `array.map(function(x){return x.toLowerCase())`. Also note that you're misusing `for..in`, you should be using a regular `for` loop to loop arrays, or `forEach` with a callback function. – elclanrs Jul 29 '14 at 17:27
  • thanks for the tip, I found THAT solution via stack overflow as well. It might have been an older solution. I will look in the array map function to ensure that it works properly for my solution. It should. – Kenneth Garza Jul 29 '14 at 17:35

3 Answers3

1

Try this on for size. To me this really has nothing to do with Angular

(function(array1, array2) {
    var tlc = function(a) { return a.toLowerCase(); };
    array2 = array2.map(tlc);
    array1 = array1.map(tlc);
    return array1.filter(function(n) {
        return array2.indexOf(n) != -1;
    }).length > 0;
})(array1, array2);
James Kleeh
  • 12,094
  • 5
  • 34
  • 61
1

Absolutely nothing to do with Angular. This is plain data structures and data manipulation. To say there should be a more AngularJS way of doing it would be like saying there should be a more MVC way to add two numbers.

Angular provides no basic data structures and utility set of functions beyond what is available in your browser's native list of array functions, which is different depending on which ECMAScript standard the browser supports.

You may want to look into a library like Lo-Dash for stuff like this (which you can use right along with Angular with no problems) as it's preferable to have proven code for these kind of data manipulations than to constantly have to debug your own.

With Lo-Dash, and remembering the requirement for case-insensitivity:

var array1Lowered = _.map(array1, function (value) { return value.toLowerCase(); });
var anyMatchesBool = _.any(array2, function (value) {
    return _.contains(array1Lowered, value);
});

Note that I'm making the assumption that there will be no non-string items in either array.

Lo-Dash normalizes the API so you don't need to worry about what functions each browswer supports. If there's a native function, Lo-Dash will use it because it's faster. If not, Lo-Dash provides an all-JavaScript implementation.

David Boike
  • 18,545
  • 7
  • 59
  • 94
-1

Using native functions...

var intersection = array1.filter(function(n) {
    return array2.indexOf(n) != -1
});

With help from Simplest code for array intersection in javascript

Community
  • 1
  • 1
Code Whisperer
  • 22,959
  • 20
  • 67
  • 85