1

I'm new to AngularJS and I'd like to build an object with data from a json file.

This is my json file:

[ {"id": 1, "name": "user1", "select": false }, {"id": 2, "name": "user2", "select": false }, {"id": 3, "name": "user3", "select": false }, {"id": 4, "name": "user4", "select": false }, {"id": 5, "name": "user5", "select": false } ] 

And now I want to use foreach loop to check which user has got select == true and push this username to new array. So here is my first Try:

'use strict'; angular.module('apiOmat', []) .controller('UsersCtrl', function($scope, $http){ $http.get('users.json').then(function(usersResponse) { $scope.users = usersResponse.data; }); $scope.submit = function(message,title){ var tempArr = []; angular.forEach($scope.users.name, function(value,key){ tempArr.push(value); }); console.log(tempArr); $scope.messagebody = '{ "title" = "' + title + '", "message" = "' + message + '"}'; } }); 

I also tried this:

 $scope.submit = function(message,title){ var tempArr = []; angular.forEach($scope.users, function(value,key){ tempArr.push( { key : value } ); }); console.log(tempArr); 

The Console logs the 5 object, but without any value. Just 1: Object 2: Object 3: Object ...

I know that the query for true or false is missing. But I want to fix this step before adding a query.

3
  • 1
    Try this angular.forEach($scope.users,function(user){tempArr.push( user)}).CommentedMar 3, 2016 at 13:21
  • Thanks Thanks Thanks! It worked almost. I just used tempArr.push( username) to get the names of my user.
    – Paili
    CommentedMar 3, 2016 at 13:25
  • this is not angular for each loop issue
    – Prasad
    CommentedMar 3, 2016 at 13:35

5 Answers 5

3

For this:

And now I want to use a foreach-loop to check which user has got select == true and push this username to my new array

$scope.submit = function(message,title){ var tempArr = []; angular.forEach($scope.users, function(item){ if( item.select == true){ tempArr.push(item); } }); 

Another simple & better solution is to use filter instead.

$scope.submit = function(message,title){ var tempArr = ($scope.users).filter(function(d){ return (d.select == true); }); }); console.log(tempArr) 
2
  • Thank you very very much :)
    – Paili
    CommentedMar 3, 2016 at 13:36
  • try second one it is more better :)CommentedMar 3, 2016 at 13:42
1

try this

 $scope.submit = function(message,title){ var tempArr = []; angular.forEach($scope.users, function(user){ tempArr.push( {"name":user.name} ); }); 
    1

    Try this one

    var tempArr = []; angular.forEach($scope.users, function(user){ user.select && tempArr.push(user.name); }); console.dir(tempArr); 
    5
    • 1
      Maybe it is because all of your users have "select" = false?? It works fine.jsfiddle.net/qp9qapgmCommentedMar 3, 2016 at 13:36
    • I copied not everything. My fault, sorry :D
      – Paili
      CommentedMar 3, 2016 at 13:41
    • Could you explain me something? I don't get that "user.select && tempArr.push(user.name);" Is that a short form for "if( item.select == true){ tempArr.push(item);"?
      – Paili
      CommentedMar 3, 2016 at 13:45
    • 1
      @Paili Yes. Javascript uses lazy evaluation of logic expressions. In my case, it firstly checks that user.select is true (or another value that converts to true, see js doc). If it is, then js goes to second expression and evaluates it. Otherwise second expression is skipped, because all result will anyway equal to true and it is unnecessary to resolve all other stuff after 'false'.CommentedMar 3, 2016 at 14:29
    • Thanks a lot :) @Boris Parnikel
      – Paili
      CommentedMar 4, 2016 at 7:31
    1

    try below

    $scope.submit = function(message,title){ var tempArr = []; angular.forEach($scope.users, function(i,j){ if(i.select == true) { tempArr.push( i.name ); } }); console.log(tempArr); 
    1
    • This solution is not checking for select == true, I guess that is the main purpose you are iterating on the array.CommentedMar 3, 2016 at 13:34
    0

    Keep it simple:

    $scope.submit = function(message, title){ var tempArr = []; angular.forEach($scope.users, function(user){ if( user.select == true) //filter select:true only tempArr.push( {"name_as_key":user.name} ); //name_as_key can be replaced if intended to }); 

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.