Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda