3

i currently have an array of objects i use to generate html checkboxes (ng-repeat). All those objects have an x.checked = true / false attribute. How can i set this x.checked = false for all objects at once?

Right now i use a simple for loop.

for(var i = 0; i < array.length; i++) {
  array[i].checked = false;
}

Does Angular have a faster or shorter way of doing this?

Thanks!

2

3 Answers 3

2

There is no way to do it per se, no matter how you write it, you will never do it all at once, it will at best be a shorthand for what your wrote above.

Sign up to request clarification or add additional context in comments.

1 Comment

They go through the entire list, it's O(n) complexity
1

Yes , You can use angular.forEach for this, Like so:

angular.forEach(array, function (item) {item.checked = false;}

2 Comments

It's also loop right? But I don't know which is faster, angular.forEach or for
yes, it is also loop. angular.forEach is slower than for.
1

Or you could use Array.forEach similar syntax to angular.forEach but I think it will be faster.

items.forEach(function (item, index, array) {
  item.checked = false;
});

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.