0

I have an array with multiple levels, each array is a "day" and each day has different events (catalogo) catalogo is an array with the food that will be served on that day. The problem that I'm having is trying to show the data in catalog;

(4) [Array(7), Array(7), Array(7), Array(7)]
    0: Array(7)
      0: {fecha: 2019, id: 1553410800, mes: 3, catalogo: Array(1), …}
      1: {fecha: 2019, id: 1553410800, mes: 3, catalogo: Array(1), …}
      2: {fecha: 2019, id: 1553410800, mes: 3, catalogo: Array(1), …}
      3: {fecha: 2019, id: 1553410800, mes: 3, catalogo: Array(1), …}
      4: {fecha: 2019, id: 1553410800, mes: 3, catalogo: Array(1), …}
      5: {fecha: 2019, id: 1553410800, mes: 3, catalogo: Array(1), …}
      6:
        fecha: 2019
        id: 1553410800
        mes: 3
        catalogo: Array(2)
          0: {id: "1553929200", tipo: "food",…}
          1: {id: "1553995800", tipo: "food" …} 
    1: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
    2: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
    3: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]

By doing this I can loop to the child array, how can I loop/access the data in catalogo;

for ( var i = 0; i < data_array.length; i++ ) {
  var childArray = data_array[i];
  for( var j = 0; j < childArray.length; j++ ) { 
}

I have tried this, but is not working:

for ( var i = 0; i < data_array.length; i++ ) {
  var childArray = data_array[i];
  for( var j = 0; j < childArray.length; j++ ) { 
    var third_Array = childArray[j];
    for ( var k = 0; k < third_Array.length; k++) { 
      console.log(third_Array);
    }
  }
}
1
  • This is a just an array of arrays, you can use the first method and it should work. Commented Mar 26, 2019 at 17:50

5 Answers 5

5

Bit hard to test without actual data but try this one:

const data = [Array(7), Array(7), Array(7), Array(7)]; // your data
data.forEach(day => {
    day.forEach(element => { 
        element.catalogo.forEach(c => { console.log(c); }))
    }
})
Sign up to request clarification or add additional context in comments.

Comments

2

You're close, however you're missing accessing the key catalogo. It should look like:

for ( var i = 0; i < data_array.length; i++ ) {
  var childArray = data_array[i];
  for( var j = 0; j < childArray.length; j++ ) { 
    var third_Array = childArray[j].catalogo; // <-- This is missing
    for ( var k = 0; k < third_Array.length; k++) { 
      console.log(third_Array[k]);
    }
  }
}

1 Comment

Thank you for your help, I was having a hard time with this
1

third_Array is actually not an array. It's an object that contains a catalogo proeprty that is an array, so you should iterate over third_Array.catalogo.

How I would write that:

 for(const weekPlan of data_array) {
   for(const dayPlan of weekPlan) {
     for(const dish of dayPlan.catalogo) {
       console.log(dish);
       //...
     }
   }
}

Or if you are only interested in the lowest level:

 for(const dish of data_array.flat().flatMap(it => it.catalogo)) {
  //...
 }

Comments

0

Based on your data structure, you need to iterate through the outer array, then iterate through each element of the inner arrays and, finally access the catalogo property of each inner array and iterate through that.

I would also simplify you syntax by utilizing the built-in Array.prototype.forEach().

const data = [
  [
    {
      catalogo: [
        { foo: 'bar00' },
        { foo: 'bar01' }
      ]
    },
    {
      catalogo: [
        { foo: 'bar02' },
        { foo: 'bar03' }
      ]
    }
  ],
  [
    {
      catalogo: [
        { foo: 'bar04' },
        { foo: 'bar05' }
      ]
    },
    {
      catalogo: [
        { foo: 'bar06' },
        { foo: 'bar07' }
      ]
    }
  ],
  [
    {
      catalogo: [
        { foo: 'bar08' },
        { foo: 'bar09' }
      ]
    },
    {
      catalogo: [
        { foo: 'bar10' },
        { foo: 'bar11' }
      ]
    }
  ]
];

data.forEach(
  (innerArray) => innerArray.forEach(
    (element) => element.catalogo.forEach(
      (item) => console.log(item.foo)
    )
  )
);

Comments

0

you seem to have an array nested with multiple arrays you can solve your problems like this here is an example

let array = [
[0, 1, 2],
[1, 2, 3],
[2, 3, 4]
]
let arrayLength = array.length;
for (let i = 0; i < arrayLength; i++) {
 let items = array[i].length;
 console.log(i, items)
 for (let n = 0; n < items; n++) {
  console.log(array[i][n]);
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.