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

215
Views
¿Cómo puedo recuperar datos de un objeto anidado dinámicamente?
I stored some object in localStorage, how i can access all of data using loop.I want to work with long data. How can i find the

solución. ** Tengo algunos datos en el objeto como: ** Del almacenamiento local: Básico:{ Pregunta:{ Pregunta1:{ Id:1, Pregunta: "¿qué es php?", }, Pregunta2:{ Id:1, Pregunta: "¿qué es js?", }, }, }′ Medio:{ Pregunta:{ Pregunta1:{ Id:1, Pregunta: "¿qué es php?", }, Pregunta2:{ Id:1, Pregunta: "¿qué es js ?", }, }, } Avanzado:{ Pregunta:{ Pregunta1:{ Id:1, Pregunta: "¿qué es php?", }, Pregunta2:{ Id:1, Pregunta: "¿qué es js?", }, }, } Html: <div class="process"> <li> Basic </li> <li> Medium </li> <li> Advanced </li> Traté de acceder como : Object obj = new Object(); $('.process li').each(function (){ obj = JSON.parse(localStorage[$(this).text()]; obj[$(this).text()]['question'].each(function(){ ¿Cómo puedo imprimir todas las identificaciones y preguntas }); }); Lo intenté de muchas maneras. Pero no obtuve la mejor solución. Por favor, ayúdenme a resolver este problema. Gracias :-)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Sería mejor reestructurar sus datos para que sea un objeto con propiedades básicas/medias que son matrices de objetos. Iterar sobre esa matriz es bastante simple.

Aquí hay un ejemplo de jQuery.

 const data = { basic: [{ id: 1, question: 'what is php?' }, { id: 2, question: "what is js?" } ], medium: [{ id: 3, question: 'what is narwhal?' }, { id: 4, question: "what is goat?" } ] }; const list = $('ul'); const keys = Object.keys(data); for (const key of keys) { const type = $(`<li>${key}</li>`); const sublist = $('<ul></ul>'); for (const q of data[key]) { const item = `<li>${q.id}: ${q.question}</li>`; sublist.append(item); type.append(sublist); } list.append(type); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul></ul>

El JS nativo es muy similar.

 const data={basic:[{id:1,question:"what is php?"},{id:2,question:"what is js?"}],medium:[{id:3,question:"what is narwhal?"},{id:4,question:"what is goat?"}]}; const list = document.querySelector('ul'); const keys = Object.keys(data); for (const key of keys) { const type = document.createElement('li'); const sublist = document.createElement('ul'); type.textContent = key; for (const q of data[key]) { const item = document.createElement('li'); item.textContent = `${q.id}: ${q.question}`; sublist.appendChild(item); type.appendChild(sublist); } list.appendChild(type); }
 <ul></ul>

about 4 years ago · Juan Pablo Isaza 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!