Dynamic array in Javascript

Asked

Viewed 5,775 times

5

I’m trying to generate a array made up of others arrays dynamically. For example:

check = function (title, filter) {
        if (checked.indexOf(title) !== -1) {
            checked[title].push(filter);
        } else {
            checked.push(title);
        }
};

check('arraypai','conteudo');
check('arraypai','outroconteudo');

So that the result would be:

[
 arraypai[conteudo,outroconteudo]
]

and then...

check('arraymae','coisa');
check('arraymae','outracoisa');

and the result would be:

[
 arraypai[conteudo, outroconteudo],
 arraymae[coisa, outracoisa]
]
  • 3

    I think what you want is a dictionary of arrays, right? Like: { arraypai:[conteudo, outroconteudo], arraymae:[coisa, outracoisa] }. Alternatively, you can have an array of name/array pairs, type: [{nome:arraypai, array:[conteudo, outroconteudo]}, {nome:arraymae, array:[coisa, outracoisa]}]. P.S. If this is the first case, simply replace checked.push(title) for checked[title] = [filter].

  • 1

    It needs to be array inside array, because then, at another time, I need to give a . Join(',') in each main array.

  • 1

    So you’re in trouble, because the most you’re gonna get is [[conteudo, outroconteudo], [coisa, outracoisa]]. There are no associative arrays in Javascript...

  • 1

    If I could create the arrays inside an object, array dictionary, as mentioned, would still work.

  • The closest to a true associative array in JS is a literal object, as suggested by @mgibsonbr. Anything, reformulates the question :D

2 answers

3


If the question is to "associate the name X with the array Y", then the best way to do it is to use an array dictionary (i.e. a simple object whose keys are names and whose values are arrays):

var checked = {};

check = function (title, filter) {
        if (checked[title] !== undefined) { // Testa se a chave existe
            checked[title].push(filter);    // Adiciona um elemento no array
        } else {
            checked[title] = [filter];      // Se não existe, cria um array com um elemento
        }
};

check('arraypai','conteudo');
check('arraypai','outroconteudo');
check('arraymae','coisa');
check('arraymae','outracoisa');

document.getElementById("saida").innerHTML = JSON.stringify(checked) + "<br/>" +
  checked["arraypai"].join(", ") + "<br/>" +
  checked.arraymae.join(", ");
<span id="saida"></span>

2

Basically everything you want to do with associative array can be done with objects.

To print, for example, you can iterate over the keys (attributes) of the object, as in the following example:

var checked = {};

var check = function (title, filter) {
    if (checked[title]) checked[title].push(filter);
    else checked[title] = [ filter ];
};

var compilar = function(){
    var str = "";
    for(var key in checked){
        str += key + "[" + checked[key].toString() + "];";
    }
    print(str + "<br />");
};

check('arraypai','conteudo');
check('arraypai','outroconteudo');
compilar();




// Função de comodidade para imprimir o resultado. Ignore-a :)
function print(source){
    var p = document.createElement("p");
    p.innerHTML = source;
    document.body.appendChild(p);
}

Browser other questions tagged

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