1

Please see below code:

var fieldsControls = $filter('filter')(vm.FieldsControls, { FieldId: id });

            for (var i = 0; i < fieldsControls.length; i++)
            {
                if (fieldsControls[i].ControlType == "DropDown") {
                    fieldsControls[i].Options = $filter('filter')(vm.Options, { type: fieldsControls[i].type });
                }
            }
            return fieldsControls;

After return (or even after the for loop) I observed that the original array "vm.FieldsControls" is getting updated with new Options value. However, shouldn't this just change new array fieldControls only?

1 Answer 1

2

Prepare deep copy of you array first. Here is the code:

     var copy = angular.copy(vm.FieldsControls);

     var fieldsControls = $filter('filter')(copy, { FieldId: id });

        for (var i = 0; i < fieldsControls.length; i++)
        {
            if (fieldsControls[i].ControlType == "DropDown") {
                fieldsControls[i].Options = $filter('filter')(vm.Options, { type: fieldsControls[i].type });
            }
        }
        return fieldsControls;
Sign up to request clarification or add additional context in comments.

2 Comments

I tried it, but it gives error as "fieldsControls is undefined".
Sorry, I used old syntax. this is as it should be: var copy = angular.copy(vm.FieldsControls); @TechTurtle

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.