-1

Hello I have an array in javascript with 3 objects in it each object has multiple sub objects and every sub object has a key and value obviously.

I am trying to loop out the subobject id of a certain key let's say for example 4 where the value is true.

What my array looks like:

myarray

I've tried this method: (result is the result of my ajax call it works)

for(var land in result.landen){
    $.each(land, function() {
        $.each(this, function(key, value) {
            console.log(key + value);
        });
    });
}

I get this error:

Uncaught TypeError: Cannot use 'in' operator to search for 'length' in 8

wilsotobianco
  • 1,360
  • 14
  • 19
Frank Lucas
  • 582
  • 2
  • 12
  • 28

1 Answers1

0

You are going 1 level too deep.

$.each(result.landen, function(key1, value1) {
    console.log(key1 + value1); 
    $.each(value1, function(key2, value2) {
        console.log(key2 + value2);        // will log "4 true"
    });
});

Your object structure is kind of weird in my opinion. Personnally, I'd rather have more explicit key/values.

{ landen :
    [
        {
            id : 8,
            type: 4
            apartements: [ //I don't know what they represent.
                {id : 8}
            ],
            warehouses: []
        }
    ]
}

Or something like that

phenxd
  • 689
  • 4
  • 17