JSFiddle - React, Tailwind, and code Playground

by chris5marsh

JavaScript

// This works
var mda1 = ['apple', 'aardvark', 'algeria'];
var test1 = 'aardvark';
var result1 = $.inArray(test1, mda1);

if (result1 > -1) {
    document.write('<p>test1 is at position '+result1+'</p>');
} else {
    document.write('<p>test1 is not in mda1</p>');
}

// This doesn't
var mda2 = [['apple', 'banana'],['aardvark', 'bear'],['algeria', 'bolivia']];
var test2 = ['algeria', 'bolivia'];
var result2 = $.inArray(test2, mda2);

if (result2 > -1) {
    document.write('<p>test2 is at position '+result2+'</p>');
} else {
    document.write('<p>test2 is not in mda2</p>');
}

// What's the solution?
(function($) {
    $.inMDArray = function(value, array) {
        var r = -1;
        $(array).each(function(i,v) {
            var s = true;
            $(v).each(function(j,w) {
                if (w !== value[j]) {
                   s = false;
                   return false;
                }
            });
            if (s === true) {
                r = i;
                return false;
            }
        });
        return r;
    };
})(jQuery);

// Tada!
var result3 = $.inMDArray(test2, mda2);
if (result3 > -1) {
    document.write('<p>test3 is at position '+result3+'</p>');
} else {
    document.write('<p>test3 is not in mda2</p>');
}