-2

How can I iterate through arrays with both objects and arrays and again nested arrays plus how to get the objects keys as well with one iteration? I know there are many posts online about nested objects/arrays but whatever I tried doesnt work and help would be great. I havent added what I tried as its all wrong, but can add it as well if anyone wants. Thanks!!

var objects =[
    {lisa:[{age:22, city: "rome", country: "italy", colorpairs:{white: ["blue","red"], price: 100}}, {age:2, city: "xxx", country: "italy"},{age:92, city: "yyy", country: "italy"}]},

  { mike:[{age:22, city: "rome", country: "italy", colorpairs:{white: ["blue","red"], price: 100}}, {age:2, city: "xxx", country: "italy"},{age:92, city: "yyy", country: "italy"}]},

    {luis:[{age:22, city: "rome", country: "italy", colorpairs:{white: ["blue","red"], price: 100}}, {age:2, city: "xxx", country: "italy"},{age:92, city: "yyy", country: "italy"}]},

  ]
javascript2016
  • 973
  • 3
  • 17
  • 41
  • 3
    Yes, go ahead and add what you tried. It might give us some idea what you are trying to do. – PM 77-1 Jan 24 '18 at 23:17
  • 3
    You should flesh out your question a bit more clearly. Maybe give us a use case? Definitely show us an example of what you want. Definitely show us something you've tried. – zfrisch Jan 24 '18 at 23:17
  • Is [this](https://stackoverflow.com/questions/8312459/iterate-through-object-properties) what you need? – GalAbra Jan 24 '18 at 23:18
  • Are you looking for this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof ? – Chris Chen Jan 24 '18 at 23:19
  • Duplicate AF.. won't even look for one cause By the time I'm done there would be 10 already.... Just Google your question and you will probably find 10+ stackoverflow results – Laurent Schwitter Jan 24 '18 at 23:30

1 Answers1

0

This is just to get you started, but you're simply going to repeat this process. For arrays, just use array.length to iterate in your for loop. For objects, you want to want to get the name with Object.keys, then use that in your next for loop to drill down into the next level. Just follow this pattern:

for (i = 0; i < objects.length; i++) {
  var name = Object.keys(objects[i])

    for (j = 0; j < objects[i][name].length; j++) {
      //keep going deeper using object.keys and repeating this

    }
}
yoursweater
  • 1,883
  • 3
  • 19
  • 41