0

I'll try to be short and get to the point.

When I choose a role, it adds it to an array with the role's name and whether it's selected or not (with true or false). Actually the array is saved the following way:

[
  {
    "name": "CDB_DBA",
    "selected": true
  },
  {
    "name": "CONNECT",
    "selected": true
  }
]

and I need it to be saved this way (with the roles I select from the checkbox list):

["CDB_DBA", "CONNECT"]

The array contains the selected roles, I'm sure it's pretty simple to change but I'm new to this and need some help. Thank you in advance. Here's the JSBin link to the project:

JSBin Project

2 Answers 2

1

I have updated your link by creating a new array and pushing the value into that array while checking the checkbox

Can you check this is expected? If not please explain it briefly.

please check the below link

http://jsbin.com/huwepirolo/edit?html,js,output

 $scope.updateArray= function(value){
      alert(value);
      $scope.array.push(value);
    }
Sign up to request clarification or add additional context in comments.

8 Comments

I've found a little bug, when I deselect one role It still adds it to the array everytime I click on the checkbox, any way to fix it? thanks in advance.
Heyy yaa i have fixed it. please check the below link I have updated the code jsbin.com/ropeqeqicu/edit?html,js,output
Ajay, I've got one last request,when I click on the edit button it brings the selected user roles, is there any way to save them in the $scope.array = [];.... It checks the checkbox roles the user has but does not update the roles inside $scope.array, I hope you understood me, thank you very much you're a life savior, I've updated screenshots to imgur to illustrate you in a better way... link @ajaykumar
Heyy @Aceman I have updated the code, Can you check is this expected or I misunderstood. jsbin.com/ropeqeqicu/edit?html,js,output
|
1

If you want to convert an array in the upper format to the lower one, you could use Array.filter to filter out just selected elements and then Array.map to pick out the name attribute:

var input = [
  {
    "name": "foo",
    "selected": false
  },
  {
    "name": "bar",
    "selected": true
  },
  {
    "name": "memes",
    "selected": false
  },
  {
    "name": "quux",
    "selected": true
  },
  {
    "name": "xyzzy",
    "selected": false
  },
  {
    "name": "corn",
    "selected": true
  }
];

var output = input
               .filter(obj => obj.selected) // filter out non-selected elements
               .map(obj => obj.name);       // just get the name
console.log(output);

Note that this leaves input unmodified; output is a new array.

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.