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

156
Views
How to work with nested loops inside nested arrays

So, I'm trying to work with nested loops to search through nested arrays, to find an specific value inside those, which is "codItem". This is the test model for the array (since I can't access the original fetch request on weekends):

let teste = [{
  item: [
    {
      codItem: 'Teste1'

    }
  ]
}, {
  item: [
    {
      codItem: 'Teste2'
    }
  ]
}, {
  item: [
    {
      codItem: 'Teste3'
    }
  ]
}];

This is the code I'm trying to use, based on some fundamental concepts and videos:

var items = teste.item;
for (i = 0; len = items.length; i < len, i++) {

  console.log(items);

  for (let properties in items[i]) {
    console.log(properties, items[i]);
  }




  var boxClonado = $("#boxMaster").clone(true);
  $(boxClonado).appendTo("#preparando").removeAttr("hidden", "style").addClass("clonados");

  $("#nomeproduto").append(teste[i].id);
  $("#quantidade").append(("Qntd.: " + teste.codItem));




}

... but at the moment, I'm getting an Uncaught TypeError: items is undefined at the first loop declaration line.

This is the last video I tried to reproduced and based the idea: https://youtu.be/AqgVLYpBWG8?t=604

Oh, the bottom part of the code refers to creating an infoBox for each item that shows up in "teste", and adding the info from "codItem" to "#nomeproduto" which is the name of the product.

Thank you in advance

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

0

teste is an array; it has no item property. You are trying to loop through undefined, thus you get the error.

Instead, loop through each item in the array, then, inside the loop, loop through each item in the current item's item property:

let teste=[{item:[{codItem:"Teste1"}]},{item:[{codItem:"Teste2"}]},{item:[{codItem:"Teste3"}]}];

for(const items of teste){
  for(const item of items.item){
    let codItem = item.codItem;
    console.log(codItem)
  }
}

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!