jQuery通过索引获取数组的值

时间:2021-04-12 14:24:43

using jQuery, I'm trying to get values of fileTypes by using an index. I'm not sure how to do go about doing this but so far I've tried settings.fileTypes[0] which does not work. Any help is appreciated, thanks!

使用jQuery,我试图通过使用索引获取fileTypes的值。我不知道该怎么做才能做到这一点,但到目前为止我已经尝试过settings.fileTypes [0]这样做不起作用。任何帮助表示赞赏,谢谢!

    var defaults = {
        fileTypes : { 
            windows: 'Setup_File.exe',
            mac: 'Setup_File.dmg',
            linux: 'Setup_File.tar.gz',
            iphone: 'iPhone App'
        }
    },
        settings = $.extend({}, defaults, options);

2 个解决方案

#1


4  

fileTypes is an Object, and as such, its properties can not be access via index number.

fileTypes是一个Object,因此,无法通过索引号访问其属性。

Objects do not guarantee any defined order, so if you need to maintain items in an indexed order, you would need to use an Array instead.

对象不保证任何已定义的顺序,因此如果您需要以索引顺序维护项目,则需要使用数组。

To get the first item given your example, you would do so by name:

要获得给出示例的第一个项目,您可以按名称进行:

settings.fileTypes.windows;  // will return 'Setup_File.exe'

To store as an Array whose items you can retrieve by index, try this:

要存储为可通过索引检索其项目的数组,请尝试以下操作:

var defaults = {
        fileTypes : [
            'Setup_File.exe',
            'Setup_File.dmg',
            'Setup_File.tar.gz',
            'iPhone App'
        ]
    },

settings.fileTypes[0];  // will return 'Setup_File.exe'

Or you could do an Array of Objects, though I don't think that's what you're after:

或者你可以做一个对象数组,虽然我不认为这是你所追求的:

var defaults = {
        fileTypes : [ 
            {type: 'Setup_File.exe'},
            {type: 'Setup_File.dmg'},
            {type: 'Setup_File.tar.gz'},
            {type: 'iPhone App'}
        ]
    },

settings.fileTypes[0].type;  // will return 'Setup_File.exe'

#2


0  

Perhaps you can use a workaround if your application design allows it. It worked for me some times.

如果您的应用程序设计允许,也许您可​​以使用变通方法。它有点适合我。

var defaults = {
    fileTypes : {
        0: {
          key: 'windows',
          file: 'Setup_File.exe'
        },
        1: {
          key: 'mac',
          file: 'Setup_File.dmg'
        }
    }
};

On that way you can access the first level elements by index (in that specific order) and if you need the keys from your example, they are still there.

在这种情况下,您可以通过索引(按特定顺序)访问第一级元素,如果您需要示例中的键,它们仍然存在。

#1


4  

fileTypes is an Object, and as such, its properties can not be access via index number.

fileTypes是一个Object,因此,无法通过索引号访问其属性。

Objects do not guarantee any defined order, so if you need to maintain items in an indexed order, you would need to use an Array instead.

对象不保证任何已定义的顺序,因此如果您需要以索引顺序维护项目,则需要使用数组。

To get the first item given your example, you would do so by name:

要获得给出示例的第一个项目,您可以按名称进行:

settings.fileTypes.windows;  // will return 'Setup_File.exe'

To store as an Array whose items you can retrieve by index, try this:

要存储为可通过索引检索其项目的数组,请尝试以下操作:

var defaults = {
        fileTypes : [
            'Setup_File.exe',
            'Setup_File.dmg',
            'Setup_File.tar.gz',
            'iPhone App'
        ]
    },

settings.fileTypes[0];  // will return 'Setup_File.exe'

Or you could do an Array of Objects, though I don't think that's what you're after:

或者你可以做一个对象数组,虽然我不认为这是你所追求的:

var defaults = {
        fileTypes : [ 
            {type: 'Setup_File.exe'},
            {type: 'Setup_File.dmg'},
            {type: 'Setup_File.tar.gz'},
            {type: 'iPhone App'}
        ]
    },

settings.fileTypes[0].type;  // will return 'Setup_File.exe'

#2


0  

Perhaps you can use a workaround if your application design allows it. It worked for me some times.

如果您的应用程序设计允许,也许您可​​以使用变通方法。它有点适合我。

var defaults = {
    fileTypes : {
        0: {
          key: 'windows',
          file: 'Setup_File.exe'
        },
        1: {
          key: 'mac',
          file: 'Setup_File.dmg'
        }
    }
};

On that way you can access the first level elements by index (in that specific order) and if you need the keys from your example, they are still there.

在这种情况下,您可以通过索引(按特定顺序)访问第一级元素,如果您需要示例中的键,它们仍然存在。