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)}). Commented Mar 3, 2016 at 13:21
  • Thanks Thanks Thanks! It worked almost. I just used tempArr.push( username) to get the names of my user. Commented Mar 3, 2016 at 13:25
  • this is not angular for each loop issue Commented Mar 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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very very much :)
try second one it is more better :)
1

try this

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

Comments

1

Try this one

var tempArr = [];
angular.forEach($scope.users, function(user){
    user.select && tempArr.push(user.name);
});

console.dir(tempArr);

5 Comments

Maybe it is because all of your users have "select" = false?? It works fine.jsfiddle.net/qp9qapgm
I copied not everything. My fault, sorry :D
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 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'.
Thanks a lot :) @Boris Parnikel
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 Comment

This solution is not checking for select == true, I guess that is the main purpose you are iterating on the array.
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
   });

Comments

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.