JavaScript Transforming array into a object in JavaScript

I am trying to convert an array into a javscript object that is designed to work with input checkboxes in AngularJS.

This is the input array that I get from my backend:

let selectedRolesDB = ['ADMIN', 'SECURITY'];

This is what my front-end expects:

let selectedRoles =
  {
    'ADMIN' : true,
    'SECURITY': true
  };

I tried different approaches such as angular.forEach but the problem is that I am not able to get the desired output:

angular.forEach(selectedRolesDB,(value, key) => {
    this.selectedRoles.push({value : true });
});

Can anyone tell me how I best solve my problem so I end up with the array that my front-end expects?

JSFiddle

Answer:1

selectedRoles is not array, it is object. Init it as empty object:

let selectedRoles = {};

angular.forEach(selectedRolesDB,(value, key) => {
    // use [] notation to add property with required name and value
    selectedRoles[value] = true;
});
Answer:2

Facing a very silly issue. I am showing alert message on a button click. It works perfectly at first click but then it is not working on next clicks, Here is my code : http://jsfiddle.net/nre6fbm5/ ...

Facing a very silly issue. I am showing alert message on a button click. It works perfectly at first click but then it is not working on next clicks, Here is my code : http://jsfiddle.net/nre6fbm5/ ...

  1. bootstrap alert not working
  2. bootstrap alert close not working
  3. bootstrap alert dismiss not working
  4. bootstrap alert-error not working
  5. bootstrap alert class not working
  6. bootstrap alert fade not working
  7. bootstrap alert css not working
  8. bootstrap alert show not working
  9. bootstrap alert close button not working
  10. react bootstrap alert not working
  11. bootstrap 4 alert not working
  12. bootstrap alert data-dismiss not working
  13. bootstrap alert message close not working

i am very new to coding so please be gentle. i have been given a project by a college that i am studying at. to build an online interactive tutorial, very much in the same vane as codecademy and the ...

i am very new to coding so please be gentle. i have been given a project by a college that i am studying at. to build an online interactive tutorial, very much in the same vane as codecademy and the ...

  1. make html editor javascript
  2. make html editor

text content of bootstrap modal body flows outside. when long text without any space. I need a solution.text content should not overlapping when text is long <script src="https://ajax....

text content of bootstrap modal body flows outside. when long text without any space. I need a solution.text content should not overlapping when text is long <script src="https://ajax....

  1. text content bootstrap
  2. text content center bootstrap
  3. carousel with text content bootstrap
  4. bootstrap text content scroll

what is the easiest way to retrieve form values to send to localStorage as a JSON string? I started a function with a for loop but am stuck..any nudges are greatly appreciated(still very new to this) ...

what is the easiest way to retrieve form values to send to localStorage as a JSON string? I started a function with a for loop but am stuck..any nudges are greatly appreciated(still very new to this) ...

  1. send html form data to email
  2. send html form to email
  3. send html form to google sheets
  4. send html form data to email using php
  5. send html form as json
  6. send html form data
  7. send html form to javascript function
  8. send html form data to email using javascript
  9. send html form to email javascript
  10. send html form data to database
  11. send html form data to node js
  12. send html form data to python script
  13. send html form data to rest api
  14. send html form data to python
  15. send html form data to excel
  16. send html form data to text file
  17. send html form data to another page
  18. send html form to email php
  19. send html form data to whatsapp
  20. send html form data to sql database