I am a beginner in AngularJS and I am trying to create an object
using data from a json
file.
This is the content of 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 }
]
My goal is to use a foreach
loop to identify which users have select == true and add their usernames to a new array
. Here is my initial attempt:
'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 attempted this approach:
$scope.submit = function(message,title){
var tempArr = [];
angular.forEach($scope.users, function(value,key){
tempArr.push( { key : value } );
});
console.log(tempArr);
When checking the Console, it shows 5 objects but without any values. It just displays: 1: Object 2: Object 3: Object ...
I understand that the logic for true or false evaluation is missing. However, I want to address this issue before implementing the query.