将数据数组传递给 jQuery 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2491238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:37:07  来源:igfitidea点击:

Pass array of data to jQuery function

jqueryfunctionarrays

提问by Ben Everard

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)

故事是这样的……我有一个 jQuery 函数可以做一些事情,这个函数经常被引用。输入参数之一是一个数组,这个数组数据是硬编码的,因此我想像这样传递数据:(这是我使用 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.

那么我如何用我的 jQuery 函数实现这一点,如果可能的话,我希望它是一个单线。

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.

简而言之,我不希望在我的函数之前有任何变量,我希望按照我给出的第一个示例中的建议,直接在函数参数中对数组进行硬编码。

:-)

:-)

回答by ground5hark

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

我重读了你编辑过的问题,你的答案在 javascript 对象中,很像其他语言的字典或哈希:

    { 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'});

回答by ant

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/

回答by David

Look into JSON formatting for your array...

查看数组的 JSON 格式...

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

回答by ground5hark

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:

如果您只引用一次这个硬编码数组,则将它作为在函数调用中声明的数组文字传递到 jQuery 函数中:

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

回答by álvaro González

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

JavaScript 没有关联数组。您可能可以通过常规数组和对象的组合获得所需的内容:

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.

有一种称为 JSON 的数据格式,它使用 JavaScript 语法。由于您似乎在使用 PHP 并且 PHP 具有生成 JSON 的内置函数在 PHP 手册中查找json_encode()),因此它可以是一项非常简单的任务:将您的 PHP 数据结构编码为 JSON 并按原样从 JavaScript 使用它。