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

134
Views
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 answers
Answer question

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 Report

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 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!