agregar una tercera columna a una matriz

I am new in jquery/javascript. I have the code below:

    var Tabs = {
    'tab1'  : 'string1',
    'tab2'  : 'string2',
    'tab3'  : 'string3'
    }

$.each(Tabs,function(i,j){      
var tmp = $('<li><a>'+i+'</a></li>');       
tmp.find('a').data('page',j);
})

I want my code to be something like this, but when I try it, it unfortunately doesnt work :

    var Tabs = {
    'tab1'  : 'string11' : 'string12',
    'tab2'  : 'string21': 'string22',
    'tab3'  : 'string31' : 'string32'
    }

$.each(Tabs,function(i,j,k){    
var tmp = $('<li><a class="'+k+'">'+i+'</a></li>');     
tmp.find('a').data('page',j);
})

In other words: I want to add a third parameter to the Tabs array. Can someone please explain to me what is the role of : . Your usual help is alwys appreciated.

preguntado el 05 de mayo de 13 a las 14:05

Cada Tabs entry should become and array, e.g. 'tab1' : ['string11', 'string12'], you should then iterate over it. -

Thank you moonwave99 for your help. It is okey -

read up on javascript objects and arrays. Not hard to find lots of basic information -

Es sintaxis literal de objeto. Note that the quotes aren’t strictly necessary. -

Thank you @rynah. That's what I was needing, these keywords as a js/jquery beginner -

2 Respuestas

The general pattern in JavaScript object literals is

var object = {
  key: value, 
  key: value
  // ...
};

So your object assignment is invalid. Furthermore, the callback passed to each() only has two parameters: index y value. So you can not just use a third parameter there.

You could, however, use an array (or another object) as value like this:

var Tabs = {
    'tab1'  : ['string11', 'string12' ],
    'tab2'  : ['string21', 'string22' ],
    'tab3'  : ['string31', 'string32' ]
    }

$.each(Tabs,function(i,j){    
  var tmp = $('<li><a class="'+j[1]+'">'+i+'</a></li>');     
  tmp.find('a').data('page',j[0]);
})

contestado el 05 de mayo de 13 a las 14:05

You can convert from key:string to a key:object pair instead. Then you can have as many values as you want. It would be something like this:

var Tabs = {
    'tab1': {
        value1: 'string1',
        value2: 'string12'
    },
    'tab2': {
        value1: 'string2',
        value2: 'string22'
    },
    'tab3': {
        value1: 'string3',
        value2: 'string32'
    }
}

contestado el 05 de mayo de 13 a las 14:05

Muchas gracias por su ayuda - Adib Aroui

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.