Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
looping through two arrays at the same time Java Script

I have one array [name1,item1,name2,item2,name3...] and need to map it into

  {
     "@type": "ListItem",
     position: index + 1,
     name: ,
     item: ,
  }

I tried to split it into two separate arrays like:

  var nameBreadCrumbStructuredData = splitBreadCrumbStructuredData.filter(
    function (value, index, Arr) {
      return index % 2 == 0;
    }
  );

  var urlBreadCrumbStructuredData = splitBreadCrumbStructuredData.filter(
    function (value, index, Arr) {
      return index % 2 == 1;
    }
  );



   var faqStructuredDataSplit = nameBreadCrumbStructuredData.map((i) => {
     urlBreadCrumbStructuredData.map((j) => ({
         "@type": "ListItem",
         position: "",
         name: i,
         item: j,
     }));
   });

but unfortunatelly it doesn't work. I tried some difrent ways and also forEach, but I'm stucked. Anyone can help me how to loop one array but every second item or two arrays at the same time? Thank you for helping!

As a result I need to get this, by mapping:

const breadCrumbStructuredData = {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    itemListElement: [
      {
        "@type": "ListItem",
        position: 1,
        name: "nameExample",
        item: "https://example.com/",
      },
      {
        "@type": "ListItem",
        position: 2,
        name: nameExampl1,
        item: "https://example1.com/",
      },
      {
        "@type": "ListItem",
        position: 3,
        name: nameExamp2,
        item: "https://example2.com/",
      },
      {
        "@type": "ListItem",
        position: 4,
        name: nameExamp3,
        item: "https://example3.com/",
      },
    ],
  };
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

how to loop one array but every second item

You can use the regular for loop.

const splitBreadCrumbStructuredData = ["name1", "item1", "name2", "item2", "name3", "item3"];
  var faqStructuredDataSplit = [];
  for(let i = 0, p = 1; i < splitBreadCrumbStructuredData.length; i += 2){
    faqStructuredDataSplit.push ({
      "@type": "ListItem",
        position: p++,
        name: splitBreadCrumbStructuredData[i],
        item: splitBreadCrumbStructuredData[i + 1],
    });
  }

  console.log(faqStructuredDataSplit);

two arrays at the same time

You don't need to loop both arrays in the map function.

var faqStructuredDataSplit = nameBreadCrumbStructuredData.map((data, index) => ({
  "@type": "ListItem",
  position:  index + 1,
  name: data,
  item: urlBreadCrumbStructuredData[index],
}));

The full code snippet

const splitBreadCrumbStructuredData = ["name1", "item1", "name2", "item2", "name3", "item3"];
    var nameBreadCrumbStructuredData = splitBreadCrumbStructuredData.filter(
      function (value, index, Arr) {
        return index % 2 == 0;
      }
    );

    var urlBreadCrumbStructuredData = splitBreadCrumbStructuredData.filter(
      function (value, index, Arr) {
        return index % 2 == 1;
      }
    );
    var faqStructuredDataSplit = nameBreadCrumbStructuredData.map((data, index) => ({
      "@type": "ListItem",
      position:  index + 1,
      name: data,
      item: urlBreadCrumbStructuredData[index],
    }));

    console.log(faqStructuredDataSplit);

about 4 years ago · Juan Pablo Isaza Denunciar

0

If I understand correctly you want to turn something like this:

["name1","value1","name2", "value2"]

into

[
  {position: 1,name: "name1",item: "value1"},
  {position: 2,name: "name2",item: "value2"}
]

This can be done in one pass without ever creating 2 separate arrays

const input = ["name1","value1","name2", "value2"];

const result = [];
for(let i=0,p=1; i<input.length;i+=2,p++){
   result.push( { position: p, name: input[i], item: input[i+1] } );
}

console.log(result);

You have a little more structure around your desired result, this is fine... just add that in:

const input = ["name1","value1","name2", "value2"];

const breadCrumbStructuredData = {
    "@context": "https://schema.org",
    "@type": "BreadcrumbList",
    itemListElement:[]
}

for(let i=0,p=1; i<input.length;i+=2,p++){
   breadCrumbStructuredData.itemListElement.push( { "@type":"ListItem", position: p, name: input[i], item: input[i+1] } );
}

console.log(breadCrumbStructuredData);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda