0
var layers = {};

//Add new layer
layer.markers = new L.Group();
layer.Name = t;

layers.layer = layer;

Gives an error layers.length is still 'undefined'. Why is it doing that ? I have verified in log that layers contains an item.

2
  • 3
    layers is not an array - it's a generic object, and objects have no length Commented Jan 23, 2014 at 12:32
  • Also where is L defined? Commented Jan 23, 2014 at 12:37

3 Answers 3

1

layers is not an array. If you want to keep it as an object literal, and check if it's empty, try defining something like this:

Object.isEmpty = function(obj) {
    for (var p in obj)
        if (obj.hasOwnProperty(p)) return false;
    return true;
};

And use

if (!Object.isEmpty(layers)) { ...

instead.

Sign up to request clarification or add additional context in comments.

Comments

0

You're not adding to the array. The last line should be:

layers.push(layer);

Comments

0

Simple question. layers is an object not an array.

What you needs to do is replace layers to following.

var layers = new Array();

And replace adding layer code from

layers.layer = layer;

to

layers.push(layer);

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.