0

I"m building a plugin for Photoshop, which is based on layers. To do this, I need to build an array with each layer which returns true when going through a function (checking the name for matches).

How could I basically "recursively" go through the object, and child objects, until I've been through everything? (Putting "selected" items in an array in the process).

The object is just a plain javascript object, with tons of objects in it (and further more inside it).

3
  • Is the problem that you're generally unfamiliar with recursion and want to know how to do it? Commented Feb 17, 2012 at 16:46
  • I'd have to say yes - I really don't have a clue how I'd do this. Commented Feb 17, 2012 at 16:50
  • Have you tried searching for the answer on StackOverflow? I did a quick search, and found lots of similar questions. Commented Feb 17, 2012 at 17:16

2 Answers 2

1

Try this:

var isASelectedLayer = function(element) {
    ...
}
var objectWithLayers = {...}

var selected = [];
var lookForSelectedLayers = function(o) {
    for(element in o) {
        if(isASelectedLayer(o[element]))
            selected.push(o[element]);
        else
            lookForSelectedLayers(o[element]);
    }
};
lookForSelectedLayers(objectWithLayers);
Sign up to request clarification or add additional context in comments.

Comments

1

Something like this, but it's missing lots of checks for now:

function showProperties(object, prefix) {
    if (typeof prefix == "undefined") prefix = "";
    var result = ""
    for (property in object) {
        result += prefix + property+"="+object[property]+" "+typeof object[property]+"\n";
        if (typeof object[property] == "object") {
            result += showProperties(object[property], prefix+"  ");
        }
    }
    return result;
}

Comments

Your Answer

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