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

190
Views
bucle javascript que envuelve cada 4 elementos en un padre

Tengo un objeto JSON que se parece a este,

 const siteMapItems = [ { section: "Facts", pages: [ { label: '1', link: '#' }, ] }, { section: "Play", pages: [ { label: '1', link: '#' }, ] }, { section: "About", pages: [ { label: '1', link: '#' }, ] }, { section: "Mission", pages: [ { label: '1', link: '#' }, ] }, { section: "Contact", pages: [ { label: '1', link: '#' }, ] },

Quiero recorrer este objeto y usarlo para generar algún resultado, quiero crear varias filas y tener 4 entradas por fila, he intentado lo siguiente,

 const renderSection = (siteMapContent) => html`${siteMapContent.map((sitemapItem, index) => { if(index % 4) { section(sitemapItem, true); } else { section(sitemapItem, false); } const section = (item, parent) => { if(parent) { return html`<div class="p-sitemap__row"><div class="p-site-map__column"> <h2 class="p-site-map__section-title">${item.section}</h2> <ul class="p-site-map__list"> ${renderList(item.pages)} </ul></div></div>`; } return html`<div class="p-site-map__column"> <h2 class="p-site-map__section-title">${item.section}</h2> <ul class="p-site-map__list"> ${renderList(item.pages)} </ul></div>`; };

Esto crea más que esto,

ingrese la descripción de la imagen aquí

¿Cuál, como puede ver, envuelve cada cuarto elemento en la fila sin crear una nueva fila con 4 elementos en ella?

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

0

Si primero agrega sus datos en una matriz de matrices (con 4 elementos por matriz interna), será mucho más fácil recorrer y crear sus secciones:

 const siteMapItems = [{section:"Facts",pages:[{label:"1",link:"#"}]},{section:"Play",pages:[{label:"1",link:"#"}]},{section:"About",pages:[{label:"1",link:"#"}]},{section:"Mission",pages:[{label:"1",link:"#"}]},{section:"Contact",pages:[{label:"1",link:"#"}]}]; const agg = siteMapItems.reduce((acc, item, idx) => { acc.curr.push(item); if (((idx % 4) == 3 || idx == siteMapItems.length - 1)) { acc.result.push(acc.curr); acc.curr = [] } return acc; }, { result: [], curr: [] }); console.log(agg.result);
 .as-console-wrapper { min-height:100% !important; }

Se convertirá en algo como:

 const section = (item) => { return html`<div class="p-sitemap__row"> ${item.map(i => { return html`<div class="p-site-map__column"> <h2 class="p-site-map__section-title">${i.section}</h2> <ul class="p-site-map__list"> ${renderList(i.pages)} </ul></div>`; }} </div>`; };
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!