0

var demoApp = angular.module('myApp', []);
demoApp.controller('MyController', function($scope, $http) {
    $scope.formData = {};
    $scope.formData.selectedTechnologies = {};

    $scope.checkSelected = function(object) {
        return Object.keys(object).some(function(key) {
            return object[key];
        });
    };

    $scope.technologies = [
      {id:1, name:'Angular JS'},
      {id:2, name:'PHP'},
      {id:3, name:'HTML'},
      {id:4, name:'JAVA'},
      {id:5, name:'CSS'},
      {id:6, name:'DOTNET'}
    ];
    
    //$scope.formData.selectedTechnologies = {'Angular JS':true, 'HTML':true}; /* This is working fine */
    
    var myTech = {"myTechnology":["Angular JS","HTML"]}; /* This is dynamic  value */
    $scope.formData.selectedTechnologies = myTech.myTechnology;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<body ng-app="myApp">
<h2>Technologies</h2>
<div ng-controller="MyController">
<form name="projectForm" novalidate ng-submit="projectForm.$valid && submitForm()">
<div class="col-sm-3" ng-repeat="technology in technologies">
<div class="checkbox checkbox-info checkbox-inline">
<input type="checkbox" name="technology_name" ng-model="formData.selectedTechnologies[technology.name]" id="technology{{$index}}" ng-required="!checkSelected(formData.selectedTechnologies)" />
<label for="technology{{$index}}" ng-bind="technology.name"></label>
<div class="col-lg-12" ng-show="projectForm.$submitted || projectForm.technology_name.$touched">
   <span ng-show="projectForm.technology_name.$error.required" class="text-danger">Select any technology.</span>
</div>
</div>
</div>
</form>
</div>
</body>

I have posted some listed of my existing technology. I want to default select Angular JS and HTML which is exist in my myTech variable.

If I write $scope.formData.selectedTechnologies = {'Angular JS':true, 'HTML':true}; I got correct output.

Please suggest me how to convert ["Angular JS","HTML"] to {"Angular JS":true,"HTML":true} ?

3 Answers 3

4

There is no need to convert your JSON object

var myTech = {
    "myTechnology": ["Angular JS", "HTML"]
  };

Just change your $scope.checkSelected

to this

$scope.checkSelected = function(object) {
        return $scope.formData.selectedTechnologies.includes(object.name);
    };

and your html to this

<input type="checkbox" name="technology_name" ng-model="formData.selectedTechnologies[technology.name]" id="technology{{$index}}" ng-checked="checkSelected(technology)" />

ECMAScript 2016 includes an include method for arrays

DEMO FIDDLE

EDIT : using indexOf as said by @tanmay in comments

$scope.checkSelected = function(object) {
        return $scope.formData.selectedTechnologies.indexOf(object.name) != -1;
    };
Sign up to request clarification or add additional context in comments.

3 Comments

..which only works in modern browsers (40+), no support in IE
You can either add polyfills or change it to indexOf in that case. This is anyway better than manipulating dynamically created array which can be used as it is
yeah good old indexOf is to go when not coding just for fun :D
2

To create such array, you can use this beautiful function called reduce.

Like this:

$scope.formData
  .selectedTechnologies = myTech.myTechnology.reduce(function(arr, tech) {
    arr[tech] = true;
    return arr;
  }, []);

Here's working snippet:

var demoApp = angular.module('myApp', []);
demoApp.controller('MyController', function($scope, $http) {
  $scope.formData = {};
  $scope.formData.selectedTechnologies = {};
  $scope.technologies = [{
      id: 1,
      name: 'Angular JS'
    },
    {
      id: 2,
      name: 'PHP'
    },
    {
      id: 3,
      name: 'HTML'
    },
    {
      id: 4,
      name: 'JAVA'
    },
    {
      id: 5,
      name: 'CSS'
    },
    {
      id: 6,
      name: 'DOTNET'
    }
  ]

  var myTech = {
    "myTechnology": ["Angular JS", "HTML"]
  };
  $scope.formData
  .selectedTechnologies = myTech.myTechnology.reduce(function(arr, tech) {
    arr[tech] = true;
    return arr;
  }, []);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<body ng-app="myApp">
  <h2>Technologies</h2>
  <div ng-controller="MyController">
    <div class="col-sm-3" ng-repeat="technology in technologies">
      <div class="checkbox checkbox-info checkbox-inline">
        <input type="checkbox" name="technology_name" ng-model="formData.selectedTechnologies[technology.name]" id="technology{{$index}}" ng-required="!checkSelected(formData.selectedTechnologies)" />
        <label for="technology{{$index}}" ng-bind="technology.name"></label>
      </div>
    </div>
  </div>
</body>

1 Comment

Thanks tanmay. Your code working fine. I have little bit change but overall your code is fit for my answer. I accept this answer. Thanks again...
1

change your array into an object with key-value pairs using map function.

var demoApp = angular.module('myApp', []);
demoApp.controller('MyController', function($scope, $http) {
    $scope.formData = {};
    $scope.formData.selectedTechnologies = {};
    $scope.technologies = [
      {id:1, name:'Angular JS'},
      {id:2, name:'PHP'},
      {id:3, name:'HTML'},
      {id:4, name:'JAVA'},
      {id:5, name:'CSS'},
      {id:6, name:'DOTNET'}
    ]
    
    var myTech = {"myTechnology":["Angular JS","HTML"]};
    var defaultTechnologies ={}
    defaultTechnologies = Object.assign(...myTech.myTechnology.map(d => ({[d]:true})))
    $scope.formData.selectedTechnologies = defaultTechnologies;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<body ng-app="myApp">
<h2>Technologies</h2>
<div ng-controller="MyController">
<div class="col-sm-3" ng-repeat="technology in technologies">
<div class="checkbox checkbox-info checkbox-inline">
<input type="checkbox" name="technology_name" ng-model="formData.selectedTechnologies[technology.name]" id="technology{{$index}}" ng-required="!checkSelected(formData.selectedTechnologies)" />
<label for="technology{{$index}}" ng-bind="technology.name"></label>
</div>
</div>
</div>
</body>

3 Comments

Please describe what you changed
You just change manually var myTech = {"myTechnology":{"Angular JS":true,"HTML":true}}; But this value is dynamic. How to change ["Angular JS","HTML"] to {"Angular JS":true,"HTML":true} through programmatically?
@Chinmay235 Revised it. Is that what you are looking for?

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.