1

I want to make an object using angular.forEach() function and then I need to push another object and initialize all the values to false but the below way points errors.

How can do this correctly? if I use "item.id" and "index.id" it will not create the intended functionality. I am attaching a photo to point out the intended functionality.link for image

        angular.forEach(metaData.validationRules, function (item, key) {
            //want to push object to another object
            angular.forEach(insarray.instances, function (index, key) {
               $scope.select = {
                    item.id: {
                        index.id:false
                    }
                }
            });
        });
5
  • I believe the way your creating your object properties is wrong, you cannot use the string value of a variable as the property name in a literal. You should use the square bracket notation against an already created object. EDIT: Also be aware if you have two item.id's with the same value the last will overwrite the others. Commented Feb 3, 2016 at 12:44
  • array.push() method doesnt apply here as this is an object Commented Feb 3, 2016 at 12:47
  • Well, you can no push object in another object. Object is a collection of key=>value. You should mention key which will hold value.. Commented Feb 3, 2016 at 12:48
  • @ste2425 no all the ids are identical here. Commented Feb 3, 2016 at 12:51
  • You could use a pair of nested reduce's on your source arrays. However i really don't understand the point in this. Maybe some context? Here's an example of reduce. No need for Angular at all. Its not the nicest thing to look at. jsfiddle.net/Lnesc2Lz Commented Feb 3, 2016 at 12:55

2 Answers 2

1

It is not available for now but with ES6 you can also use this syntax.

angular.forEach(metaData.validationRules, function (item, key) {
  //want to push object to another object
  angular.forEach(insarray.instances, function (index, key) {
    $scope.select={
      [item.id]: {
        [index.id]:false
      }
    }
  });
});

More example here

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

Comments

0

The problem is that it's not possible to set the key of a key-value-pair the way you do it in your example. A better approach would be:

$scope.select = {};
angular.forEach(metaData.validationRules, function (item, key) {
    $scope.select[item.id] = $scope.select[item.id] || {};

    angular.forEach(insarray.instances, function (index, key) {
        $scope.select[item.id][index.id] = false;
    });
});

Consider the declaration of the new select object outside of the loops. This is important, otherwise you would overwrite the object on each cycle.

4 Comments

Uncaught TypeError: Cannot set property '3' of undefined, where index.id is 3. You have to do this first: $scope.select[item.id] = {}
I'd recommend this: $scope.select[item.id] = $scope.select[item.id] || {}
I tried using both second and first method.But still getting that error Uncaught TypeError: Cannot set property '3' of undefined.Also i declared $scope.select ={} outside the loop.
Reduce would get around the issue of creating empty objects if they do not exist. Also if validationRules and instances is an array just use validationRule.forEach its native. No need to make your code dependent on angular for no reason.

Your Answer

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