Asked  7 Months ago    Answers:  5   Viewed   47 times

Is there a way in JavaScript to compare values from one array and see if it is in another array?

Similar to PHP's in_array function?

 Answers

77

No, it doesn't have one. For this reason most popular libraries come with one in their utility packages. Check out jQuery's inArray and Prototype's Array.indexOf for examples.

jQuery's implementation of it is as simple as you might expect:

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}

If you are dealing with a sane amount of array elements the above will do the trick nicely.

EDIT: Whoops. I didn't even notice you wanted to see if an array was inside another. According to the PHP documentation this is the expected behavior of PHP's in_array:

$a = array(array('p', 'h'), array('p', 'r'), 'o');

if (in_array(array('p', 'h'), $a)) {
    echo "'ph' was foundn";
}

if (in_array(array('f', 'i'), $a)) {
    echo "'fi' was foundn";
}

if (in_array('o', $a)) {
    echo "'o' was foundn";
}

// Output:
//  'ph' was found
//  'o' was found

The code posted by Chris and Alex does not follow this behavior. Alex's is the official version of Prototype's indexOf, and Chris's is more like PHP's array_intersect. This does what you want:

function arrayCompare(a1, a2) {
    if (a1.length != a2.length) return false;
    var length = a2.length;
    for (var i = 0; i < length; i++) {
        if (a1[i] !== a2[i]) return false;
    }
    return true;
}

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(typeof haystack[i] == 'object') {
            if(arrayCompare(haystack[i], needle)) return true;
        } else {
            if(haystack[i] == needle) return true;
        }
    }
    return false;
}

And this my test of the above on it:

var a = [['p','h'],['p','r'],'o'];
if(inArray(['p','h'], a)) {
    alert('ph was found');
}
if(inArray(['f','i'], a)) {
    alert('fi was found');
}
if(inArray('o', a)) {
    alert('o was found');
}  
// Results:
//   alerts 'ph' was found
//   alerts 'o' was found

Note that I intentionally did not extend the Array prototype as it is generally a bad idea to do so.

Wednesday, March 31, 2021
 
radmen
answered 7 Months ago
15

As Jani said, this is not possible directly with JSON, but this might help you: http://web.archive.org/web/20080828165256/http://solutoire.com/2008/06/12/sending-javascript-functions-over-json/

Wednesday, March 31, 2021
 
Student
answered 7 Months ago
61

If a function is defined at the global level, then it automatically becomes a child of the window object.

Therefore you can always call window.functionName(); any place you would normally just call functionName();.

Further, since in Javascript objects work like associative arrays, you can call any child of any object using array syntax like this: object['childName']. This includes functions, so you can do object['functionName'](); for any function which is a member of an object.

Combining these two points together, you can call any globally defined function like so:

window['functionName']();

And since functionName in the above example is a string, you can use a variable in those brackets, which means you've got the same functionality as PHP's call_user_func().

[EDIT]

As I stated, this works for any object. The OP's comments state that the functions he wants to use this way are in a JQuery plug-in. They are therefore likely to be part of the JQuery object, and would normally be called like so: JQuery().functionName(); (or with the $ in place of JQuery).

Javascript syntax allows us to use ['functionName']() in any place where we can use .functionName(), so therefore, taking the above JQuery example, we could change it to look like this:

JQuery()['functionName']();`

But this technique can be adapted for any Javascript object. Any place where you use .functionName(), it can be replaced with ['functionName']().

Wednesday, March 31, 2021
 
tedders
answered 7 Months ago
44

Save thumbnails to disk. Image processing takes a lot of resources and, depending on the size of the image, might exceed the default allowed memory limit for php. It is less of a concern if you have your own server with only your application running but it still takes a lot of cpu power and memory to resize images. If you're considering creating thumbnails on the fly anyway, you don't have to change much - upon the first request, create the thumbnail from the source file, save it to disk and upon subsequent requests just read it off the disk.

Wednesday, March 31, 2021
 
smiggle
answered 7 Months ago
13

This is explained in their documentation. Try this:

var hash = CryptoJS.HmacSHA256("Message", "Secret Passphrase");

var base64 = hash.toString(CryptoJS.enc.Base64);

You need to include http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js for this. If you didn't include this, CryptoJS.enc.Base64 will be undefined and fallback to the default.

Working demo: http://jsfiddle.net/ak5Qm/

Saturday, May 29, 2021
 
Kevin_Kinsey
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :