Home > database >  looping through two arrays at the same time Java Script
looping through two arrays at the same time Java Script

Time:11-20

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/",
      },
    ],
  };

CodePudding user response:

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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);
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related