2

I want to add a color column dynamically to the "solutions" table. My code is based on angularjs and json. I tried the following:

$scope.solutions.push({"color": value2.color});

The value2.color is dynamically decided by a forEach loop, but I was not able to add new column, did i do something wrong? Here is the Plunker URL: http://plnkr.co/edit/5gK5cBRdLQ6s8Gyy6ad8?p=preview

thanks!

10
  • it's correct. Is it show any error? Commented Mar 19, 2015 at 4:40
  • 2
    can you post a link to your full javascript source? perhaps a plunker Commented Mar 19, 2015 at 4:43
  • Is the reason you are calling it value2 that it is the second argument provided by the foreach? The second argument is the key, try value1.color. Just a thought. Commented Mar 19, 2015 at 4:50
  • 2
    did you initialized the scope variable? like $scope.house=[] .... Commented Mar 19, 2015 at 5:39
  • @vamsikrishnamannem thanks for the quick reply, but no..there are no errors in the console. Commented Mar 19, 2015 at 14:02

3 Answers 3

4

Instead of pushing the object to the array, you need to get the object at that index and add the new property.

angular.forEach($scope.solutions, function(value1, i) {
    angular.forEach($scope.eliminations, function(value2, j) {
        if (value1.alien === value2.alien && value1.world === value2.world){
            $scope.solutions[i].color = value2.color;
        }
    });
});

Plunker http://plnkr.co/edit/reLCNHT1XvVuwRSF3873?p=preview

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

3 Comments

Thanks! It seems working on plunker, but for some reason, it does not even execute the forEach loop if i don't manually put those two arrays there. even if i hard coded those two arrays, the color value could not pass to the view when i filter it by name.
alright! it works! i made a silly mistake by putting the loop in wrong place. thanks so much! but why .push did not work, while the index way works?
@Lisa push appends the item to the array, so it's not modifying the object in the array.
0

It's hard to edit your plunker as I can't call pull your solutions or eliminations data structures. But as Ramesh Rajendran said above, the .push only works for initialized arrays

You need to create an array on your scope

  $scope.solutionsArray = [];

and instead of pushing to $scope.solutions you need to push to $scope.solutionsArray.

You also need to update your view by adding soultionsArray to your ng-reapeat

  <tr ng-repeat="solution in solutionsArray | filter:SelectedStudent.student_name | filter: {student_name: ''}" >

This is the best I could do without seeing your data structures. I hope this helps! goood luck

1 Comment

hi, Aaron, thank you for the reply! I updated the plunker, and i believe that $scope.solutions is a json array. and i wanted to push the color filed in eliminations array to solutions.
0

You probably want to update your code to something like this:

angular.forEach($scope.solutions, function(value1, key1) {
    angular.forEach($scope.eliminations, function(value2, key2){
        if(value1.alien === value2.alien && value1.world === value2.world){
            console.log(value2.color);
            // value1 is a solution object in the solutions array
            value1["color"] = value2.color;

        }
    });
});

1 Comment

right...but i don't have the"color" key in solutions array, so it did not work...thanks though

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.