How to create an array within another array with jQuery

Asked

Viewed 16,826 times

6

I’m trying to create a array in jQuery using the . push() function where each element should contain another array within it. The structure I need to create is this:

 
array(
    'elemento1' => array(
        'elemento1' => 'dado1',
        'elemento2' => 'dado2'
    ),
    'elemento2' => array(
        'elemento1' => 'dado1',
        'elemento2' => 'dado2'
    ),
    'elemento1' => array(
        'elemento1' => 'dado1',
        'elemento2' => 'dado2'
    ),
) 

My intention is to generate a list of information with the invoices of an NF-e, where each invoice will have its invoice number, expiration date and its value. With this I want to generate an array in jquery to pass via ajax as parameter and in php file, do all the processing to be written to mysql. Basically that’s it.

How to proceed?

  • Bruno saw your fiddle now, there are some simple bugs to solve. Can you give an example of how you want to use this array? so it’s easier to get the answer right.

  • My intention is to generate a list of information with the invoices of a NF-e, where each invoice will have its invoice number, expiration date and value. With this I want to generate a array in the jquery to pass via ajax as a parameter and in the php file, do all the processing to be written to mysql. Basically that’s it.

3 answers

6

There are several ways. The one closest to the code you put in (which is PHP, right? ), are nested literal objects (if keys have names), or nested arrays (numeric keys).

Nested literal objects

var obj = {
    'elemento1' : {
        'elemento1' : 'dado1',
        'elemento2' : 'dado2'
    },
    'elemento2' : {
        'elemento1' : 'dado1',
        'elemento2' : 'dado2'
    },
    'elemento1' : {
        'elemento1' : 'dado1',
        'elemento2' : 'dado2'
    },
};

Nested literal arrays

var arr = [
    ['dado1','dado2'],
    ['dado1','dado2'],
    ['dado1','dado2']
];

Or maybe you want a combination between the two, as in answer by Eduardo Nobre.

Using push

Since you quoted it push in the question, here are some examples with this method:

// Primeiro você cria a array, depois dá push dos valores
var a = [];
a.push('dado1', 'dado2');

// O resultado é o mesmo de usar arrays literais:
var b = ['dado1','dado2'];
var c = ['dado1','dado2'];

// Embrulhando tudo em outra array
var arr = [];
arr.push(a, b, c);

Note: arrays are not jQuery, they are part of the Javascript language (in which jQuery is written).

1

You can do something like this:

var options = { 
    size: ["S", "M", "L", "XL", "XXL"],
    color: ["Red", "Blue", "Green", "White", "Black"]
};

To access each key individually:

for (var key in options) {
    alert(key);
}
  • 1

    Eduardo Nobre I tried to implement his logic but without success this link was as far as I could do. And then you tell me something about: https://jsfiddle.net/u49zt8wr/3/

1


Browser other questions tagged

You are not signed in. Login or sign up in order to post.