Producing a nested data set from flat data I have this example data :

 var objects = [
                {
                    title: "Parent Area",
                    code: "P",
                    areas: "B,C",
                    child: [],
                    selected: 0,
                    remove: 0
                },
                {
                    title: "Child area one",
                    code: "B",
                    areas: "C",
                    child: [],
                    selected: 0,
                    remove: 0
                },
                {
                    title: "Child area two",
                    code: "C",
                    areas: undefined,
                    child: [],
                    selected: 0,
                    remove: 0
                }
            ];


I want to create this data set :

sortedSet = [
                {
                    title: "Parent Area",
                    code: "P",
                    areas: "B,C",
                    child: [
                        {
                            title: "Child area one",
                            code: "B",
                            areas: "C",
                            child: [
                                {
                                    title: "Child area two",
                                    code: "C",
                                    areas: undefined,
                                    child: [],
                                    selected: 0,
                                    remove: 0
                                }
                            ],
                            selected: 0,
                            remove: 0
                        },
                        {
                            title: "Child area two",
                            code: "C",
                            areas: undefined,
                            child: [],
                            selected: 0,
                            remove: 0
                        }

                    ],
                    selected: 0,
                    remove: 0
                }
            ];


Iv written the following code: 

 /**
             * for each area get it's children
             */
            angular.forEach(objects, function (obj) {

                if (obj.areas) {

                    var subChildren = obj.areas.split(",");

                    /**
                     * assign the current child as child of the parent area
                     */
                    angular.forEach(subChildren, function (currentChild) {

                        angular.forEach(objects, function (currentObj) {

                            if (currentObj.code == currentChild) {

                                /**
                                 * mark this for removal from main array
                                 * @type {number}
                                 */
                                currentObj.remove = 1;
                                obj.child.push(currentObj);
                            }
                        })

                    })

                }

            })

            var sortedSet = [];
            /***
             * clean out main areas array
             */
            angular.forEach(objects, function (obj, index) {

                if (!obj.remove) {
                    /**
                     * copy the correct values to a secondary array
                     */
                    sortedSet.push(objects[index]);
                }
            })


My code works as required but I really wish I didn't need to do so many loops.

Any suggestion re-factors to optimise this code?
The number of nested loops can be reduced and doing so provides an opportunity to check your data for duplicate ids. This may not be necessary if you trust your source to provide good data.

var objectByCode={};

objects.forEach(function(o){
    // if( objectByCode[o.code] ) throw 'Duplicate codes';
    objectByCode[o.code]=o;
});

objects.forEach(function(o){
    if(o.areas){
        o.areas.split(",").forEach(function(c){
            if(objectByCode[c]){
                o.child.push(objectByCode[c]);
                objectByCode[c].remove=1;
            } // else { throw 'Code not found'; }
        });
    }
});

var sortedSet=objects.filter(function(o){ return !o.remove; });


Personally I would worry less about how many nested loops you have used and more about the potentially recursive data structure you are creating. This may prove difficult to work with if any of the children turn out to be their own ancestors. If for example the object with code C had area P then the resulting sortedSet would be empty. If B was a child of C then any naive attempt to list all descendants of B would result in a infinite loop.

If you are sure that your data will never contain this situation then you should be safe. If you are worried that this problem might occur a quick and dirty way to test for it is to use JSON.stringify which will throw an error when asked to produce the string equivalent of recursive objects.

try{
    JSON.stringify(sortedSet);
}catch(e){
    // how you decide to handle this is up to you
}


It might not be important to your application if this is the case, but be aware that your code may need to handle these situations.