5

Here's the story... I have a jQuery function that does something, this function is referenced very regularly. One of the input parameters is an array, this array data is hard coded and thus I want to pass the data like this: (this is how I would do it using PHP)

myFunction(Array("url"=>"abc.com","example"=>"hello_world"));

Instead of

$arr=Array("url"=>"abc.com","example"=>"hello_world");
myFunction($arr);

So how do I achieve this with my jQuery function, I want this to be a one liner if possible.

Edit

Maybe my example was a bit misleading, take a look at the array indexes again. The array I am sending is an associative array.

In short I do not want any variables before my function, I want the array to be hardcoded directly in the function parameter as suggested in the first example I gave.

:-)

5 Answers 5

7

I reread your edited question, and your answer is in the javascript object, much like dictionaries or hashes in other languages:

    { first_key: '1', second_key: '2', third_key: '3' };

And for your callback, just pass it in as a literal declared on the spot:

    myFunction({ first_key: '1', second_key: '2', third_key: '3'});
Sign up to request clarification or add additional context in comments.

Comments

1

Here is example if this is what you mean:

    var arr = [ "one", "two", "three", "four", "five" ];

    jQuery.each(arr, function() {
          $("#" + this).text("Mine is " + this + ".");
           return (this != "three"); // will stop running after "three"
       });

//Source http://api.jquery.com/jQuery.each/

1 Comment

Thanks for your answer, unfortunately I think my question was slightly misleading and thus this isn't relevant to my problem. Thanks any way though :)
1

Look into JSON formatting for your array...

somefunction({'key1':'value', 'key2':'value'});

3 Comments

Thanks for your answer, I tried this func(['key1':'value', 'key2':'value']); but get this error missing ] after element list [Break on this error] getElementContents(['key1':'value', 'key2':'value']); :S
This worked great but bobthabuilda got there first. Many thanks for your help :)
Just FYI, that's not valid JSON. It's a valid JavaScript object literal, but JSON is much stricter and requires double quotes (") for key names and strings.
0

I'm not entirely sure what you're asking here, but I'll give answering it a shot. If you're referencing this array in more than one place then it's probably best to define it as a constant first:

    ARRAY = ['a', 'b', 'c']
    myFunction(ARRAY);
    myFunction2(ARRAY);

If your only referencing this hard-coded array once, then pass it into the jQuery function as an array literal declared in the function call:

    myFunction(['a', 'b', 'c']);

1 Comment

Hi there, your last example is almost exactly what I want to do, I don't want any variables before my function, but it has to be an associative array. Thanks again :)
0

JavaScript does not have associative arrays. You can probably get what you are asking for with a combination of regular arrays and objects:

var foo = [
    {
        url: "abc.com",
        example: "hello_world"
    },
    {
        url: "example.net",
        example: "example"
    }
];

There is a data format called JSON that uses JavaScript syntax. Since you seem to be using PHP and PHP has builtin functions to generate JSON (find json_encode() in the PHP manual) it can be a very simple task: encode your PHP data structure as JSON and use it as is from JavaScript.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.