Getting the checked values from an AngularJS Material checkbox

<md-checkbox ng-repeat="program in ctrl.programs" ng-model="ctrl.programsSelected[program.id]">
  {{program.name}}
</md-checkbox>

Checked Items:

{{ctrl.programsSelected | json}}

Current Output:

Checked Items:
[null,true,true,true,null,true,null,true,null,true,null,null,true] 

How can I retrieve the list of checked values?

Answer №1

To narrow down the original array ctrl.primaryProgramStudies, you can filter it based on whether the corresponding index in

ctrl.primaryProgramStudiesSelected
is set to true.

var ctrl = {};

ctrl.primaryProgramStudies = [{
    "name": "test0",
    "id": 0
  },
  {
    "name": "test1",
    "id": 1
  },
  {
    "name": "test2",
    "id": 2
  },
  {
    "name": "test3",
    "id": 3
  },
  {
    "name": "test4",
    "id": 4
  },
  {
    "name": "test5",
    "id": 5
  },
  {
    "name": "test6",
    "id": 6
  },
  {
    "name": "test7",
    "id": 7
  },
  {
    "name": "test8",
    "id": 8
  },
  {
    "name": "test9",
    "id": 9
  },
  {
    "name": "test10",
    "id": 10
  },
  {
    "name": "test11",
    "id": 11
  },
  {
    "name": "test12",
    "id": 12
  }
]

ctrl.primaryProgramStudiesSelected = [null, true, true, true, null, true, null, true, null, true, null, null, true]

ctrl.selectedValues = ctrl.primaryProgramStudies.filter(function(obj, index) {
  return ctrl.primaryProgramStudiesSelected[index] === true
})

console.log(ctrl.selectedValues)

Answer №2

To filter elements in an array based on a condition, you can utilize the `filter` method along with a `callback` function.

By using the `filter()` method, a new `array` is created containing only the elements that satisfy the condition specified in the `callback` function.

var array=[null,true,true,true,null,true,null,true,null,true,null,null,true];
ctrl.primaryProgramStudies.filter(function(item,index){
    return array[index]==true;
});

Here's a brief example:

var ctrl = {};

ctrl.primaryProgramStudies = [{
    "name": "program0"
  },
  {
    "name": "program1"
  },
  {
    "name": "program2"
  },
  {
    "name": "program3"
  },
  {
    "name": "program4"
  },
  {
    "name": "program5"
  },
  {
    "name": "program6"
  },
  {
    "name": "program7"
  },
  {
    "name": "program8",
  },
  {
    "name": "program9",
  },
  {
    "name": "program10",
  },
  {
    "name": "program11",
  },
  {
    "name": "program12"
  }
]

ctrl.primaryProgramStudiesSelected =[null,true,true,true,null,true,null,true,null,true,null,null,true];
var result=ctrl.primaryProgramStudies.filter(function(item,index){
     return ctrl.primaryProgramStudiesSelected[index]==true;
});

console.log(result)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Changing Meteor Template to a Node.js/AngularJS Conversion

My current website is built with Meteor using templates structured like this: <body> {{>section1}} {{>section2}} {{>section3}} </body> <template name="section1"> <h1>This is Section 1</h1> </template> ...

Exploring the depths of jQuery promises

My journey into the world of promises has just begun, and I must say it's been quite intriguing. However, there are some questions that have been lingering in my mind. It seems to me that $.Deferred().promise, $.get().promise, and $.fn.promise().pro ...

Using Ajax without implementing JavaScript

Is it possible to develop an application that utilizes Ajax without relying on JavaScript, allowing it to function even if JavaScript is disabled by the user in their browser? Are there any restrictions or limitations to consider? ...

Ways to reset a JQuery/JavaScript filter

I am currently working on a list of div items that are being filtered and sorted using JavaScript. Each item animates as it enters the screen, scaling up to its final size. However, when I apply filters using buttons, items that are already displayed do n ...

Optimal method for presenting informative content at the top of a webpage: Harnessing the power of JavaScript

Could you check out the image linked above? There appears to be a css animation on the bright yellow arrow. Issue: I am working on a page where I need to guide users' attention (possibly with a yellow arrow) and have it disappear automatically ...

The getElementBy method is failing to pass the value in the document

Here is the javascript code snippet I am using: document.getElementById('district').value = dist.long_name "this line is passing the value" document.getElementById('city').value = route.long_name "this doesn&apos ...

Looking for a dynamic solution to retrieve over 100 data products in Angular JS? Let's explore methods to efficiently call a large volume of

Just starting out with Angular JS and working on creating a searchable product list gallery using Angular JS. Currently, all my product data is in the same js file which I know isn't the best solution. I would like to make it dynamic by connecting to ...

Generate an image of a "button" with dynamic hover text

I am trying to create a clickable image with dynamically changing text overlaid on it. I have the PHP code that retrieves the necessary information for the text, but I am struggling to create the button as I am new to this type of task. Here is an example ...

Ways to Implement an Origin's First-Party Cookies While Using it as a Third-Party

Imagine I am the owner of a website called "treat1creator.com". I want my clients, who are also website owners, to send HTTP requests to my server that contain cookies I have generated. Here is how it works: User "Sally" visits my site at treat1creator.co ...

What amount of memory is required for the largest possible array?

Is it possible to calculate the exact amount of memory required to create the largest possible array in Java? Example Program: public class MemoryOfArraysTest { public static void main(String... args) { Object[] array = new Object[Integer.MAX_ ...

Tips for preventing the use of eval when invoking various functions

Here's a snippet of code I've been using that involves the use of eval. I opted for this approach as it seemed like the most straightforward way to trigger various factory functions, each responsible for different web service calls. I'm awa ...

Tips for sending textarea data via AJAX with TinyMCE

Recently, I encountered an issue with submitting forms using the TinyMCE Jquery plugin. While regular input fields like text fields and select boxes submit just fine, there seems to be a glitch when it comes to submitting a textarea with TinyMCE. It requir ...

Using JavaScript, add a complex JSON record to a JSON variable by pushing it

I have been attempting to insert a complex JSON data record into a JSON variable using the following code: var marks=[]; var studentData="student1":[{ "term1":[ {"LifeSkills":[{"obtained":"17","grade":"A","gp":"5"}]}, {"Work":[{"obtained":"13"," ...

Tips for creating a pop-up window on an ASP.NET web form

I am looking to incorporate a popup window as a child of my primary asp.NET form. The popup window will allow users to input information using a dropdown list. When the popup window appears, it will take focus and disable the main window. ...

Transferring Command Line Arguments to a New Array

My goal is to create an array copy of the arguments received via command line in a program using malloc(). For example, if I run ./a.out one two three I would like to have an array containing {a.out, one, two, three}. Unfortunately, my current implementa ...

The absence of a React JS button on the page

I'm in the process of creating a React demo application and I've come across this component: var MediaList = React.createClass({ displayName: 'MediaList', getInitialState: function() { return { tweets: getTweets(numTweets) ...

Adjust the appearance of a div according to the input value

When a user inputs the correct value (a number) into an input of type "number," I want a button to appear. I attempted var check=document.getElementById("buttonID").value == "1" followed by an if statement, but it seems I made a mistake somewhere. Here&ap ...

Leverage AJAX on the client-side for optimal performance while utilizing node.js

I am currently working on a simple nodejs application that displays HTML pages. Within this application, there are two buttons that are linked to JavaScript functions in separate files. One of the functions uses ajax, while the other does not. However, I a ...

locating the truth value of the data in an array retrieved from MongoDB

When returning from the mongoose find() function, I need to ensure that is_reqestor = true is checked. However, when updating the document, I pass the id which needs to be updated. let filter = { is_reqestor: true } if (!is ...

Using Javascript to dynamically copy text to the clipboard

Is there a way to create a function that can automatically copy the current URL in the address bar to the clipboard? Before jumping to conclusions, hear me out: I'm looking for a solution where the copying process happens dynamically, not just when ...