Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
JavaScript - ¿Usando una variable para referirse a un nombre de propiedad?

Estoy creando mi propio objeto:

 gridObject = new Object();

Luego estoy usando jquery para extraer el contenido de las etiquetas de los elementos de la lista, que a su vez están llenos de

etiquetas que tienen nombres de clase específicos:

 <li row="1"><p class="department" rowitem="department">Photography</p>...</li>

Los estoy tirando usando este código:

 //make object from results gridObject = new Object(); //get all the rows var rowlist = $('li[row]'); for(var r=0; r<rowlist.length; r++) { //make gridObject row element here //get the row content var thisrow = $(rowlist[r]).html(); //get all the p tags var rowitems = $(thisrow + 'p.[rowitem]'); //get field name for(var ri=0; ri<rowitems.length; ri++) { if (r < 2) { //this is temporary just for testing var fieldname = $(rowitems[ri]).attr('rowitem'); var fieldvalue = $(rowitems[ri]).html(); } }

Me estoy obsesionando al pasar esto a mi objeto. Dos preguntas. ¿Se puede hacer una propiedad de objeto con un nombre de variable, así?

 griObject.fieldname = fieldvalue;

y pueden los objetos tener relaciones padre/hijo como:

 gridObject.r.fieldname = fieldvalue;

en este caso, tanto r como fieldname serían variables. ¿O debería estar trabajando en matrices asociativas para lograr algo similar?

Esto es en respuesta a una pregunta de seguimiento que publiqué a continuación: "¿Hay un equivalente de print_r en javascript"? Puede usar el iterador, escribir un poco más pero funciona:

 //loop through search data var it = Iterator(filteritems); for(var pair in it) { console.log("key:" + pair[0] + ", value:" + pair[1] + "\n"); }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Si desea utilizar un nombre de propiedad variable, utilice la sintaxis de subíndice:

 var fieldname = 'test'; //These two lines are equivalent as long as fieldname is 'test': gridObject[fieldname] = fieldvalue; gridObject.test = fieldvalue
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!