How to check all array equal to some value in angular js? - angularjs

self.scope.savesectioneditmain = [self.scope.savesection2details,
self.scope.editsection2,
self.scope.savesection1details,
self.scope.editsection1];
Based on above codes, I want to check all array value equal to true value.
if($.inArray(true, self.scope.savesectioneditmain) == 0)
I have tried $inArray, but $inArray checks only one condition, but I want to check all array value should be equal to true.

You can use Array.prototype.every()
The every() method tests whether all elements in the array pass the test implemented by the provided function.
if(self.scope.savesectioneditmain.every(x => x == true)){
//All elements are true
}

Related

How to initial BooleanArray by specific size

I have read Kotlin doc
<init>(size: Int, init: (Int) -> Boolean)
Creates a new array of the specified size, where each element is calculated by calling the specified init function.
The function init is called for each array element sequentially starting from the first one. It should return the value for an array element given its index.
Common
JVM
JS
Native
(size: Int)
Creates a new array of the specified size, with all elements initialized to false.
Constructor Creates a new array of the specified size, with all elements initialized to false.
<init>(size: Int)
Creates a new array of the specified size, with all elements initialized to false.
Constructor Creates a new array of the specified size, with all elements initialized to false.
also try simple code
var booleanArray = <init>(30)
it still not working. any help?
In the documentation, <init> refers to a constructor. You should replace it with the name of the class, which is how you call a constructor. In this case:
var booleanArray = BooleanArray(30)
var booleanArray = BooleanArray(candies.size)
This is how I init dynamic array
Tenfour04's answered your question about <init>, but just to be clear: wherever it talks about "the function init" it's talking about the init parameter in one of the constructors. This is a function which takes an Int (which will be an index in the array) and returns a Boolean value (which will be the value at that index in the array).
So you can make this extremely very useful array:
val oddNumbers = Boolean(100) { it % 2 == 1 }
which will pass in index 0, do 0 % 2 and get 0, which isn't equal to 1, so index 0 is set to false, then the same for index 1 which ends up true, and so on.
Or you can ignore the index and just set the same value for everything:
val allTrue = Boolean(100) { true }
All the array constructors work like this, so you can easily set a default value or fill each index with different values

Why two arrays of objects are not equal in angularjs?

I can't understand why it's always shows NOT equal in code :
if(JSON.stringify(data.content.items) != JSON.stringify(updatedItems)) {
console.log('update');
updatedItems = data.content.items; // updatedItems -global variable
}
I receive array of objects and check every second if it's equal or not.
Use angular.fromJson(json) instead. It will strip the $$hashKey, that's making it not equal

Assign a value at index out of bound

I have an array and I first check if the array has the index of given number. I like to assign a value at that index if the array has not got any.
var newArray:Array = [0,1,2,3];//length is 4
if(newArray[5] == "")//true
{
newArray[5] = 5;
}
Adobe Help page says;
Inserting array elements
...
If the Array or Vector doesn’t already have an element at that index, the index is created and the value is stored there. If a value exists at that index, the new value replaces the existing one.
But I am not sure it is about null elements or undefined.
How can I assign a value to index that doesn't exist?
I can push till the given index but wondering is anything else possible.
array reference its elements as not typed so if an element does not exist it can be only undefined (default value for untyped) so in your case no need to check for null or "", you only need to check for undefined.
if(newArray[5] == undefined)
{
newArray[5] = 5;
}
EDIT:
undefined is a keyword in as3 that defines a default value for untyped objects. It is the value you use to check if an untyped object has no current value. (As opposed to a typed object that has null as default value with the exception of numbers).
Using "" as you suggest doesn't work since it is a valid String value and would only work to check if a String object is not null and has a length of 0. Equivalent of String.length == 0.
The assignment is correct, even though the index 4 does not exist at this point the Array Object does not complain and assign the value to index 5.

How to check array is not NULL using AngularJS?

I have this array:
var someArr = [1,2,3,4,5];
At some point I need to check that array is not null or length of the array > 0.
What is elegant way to implement it in Angularjs?
You can just check if the variable (array,object anything) has a truthy value or not. That means :
if( value ) {
}
will evaluate to true if value is not:
1. null
2. undefined
3. NaN
4. empty string ("")
5. 0
6. false
This is for above six conditions. But in case you have array define as:
var arr = [];
then you need to check arr.length
This question is more related to JavaScript, not AngularJS.
function nonEmpty(arr) {
return !!arr && arr.length > 0;
}
Examples:
nonEmpty(['a']) // true
nonEmpty([]) // false
nonEmpty(null) // false
nonEmpty(undefined) // false
I guessing you mean something like the following:
angular.isDefined()
Which is archievable by defining it yourself at the root of your application
angular.isNotNullOrZero = function(array) {
return !!array && !!array.length;
}
So you can use it throughout your application.
var someArr = [1,2,3,4,5];
console.log(angular.isNotNullOrZero(someArr);
etc..
But like others said, this is just plain JS.

Search if a specific attribute in all the elements of an array match a condition

I'm working with Swift, SpriteKit and Xcode 6,
I have an array of a SKSpriteNode class :
class EnemyVehicle: SKSpriteNode {
}
var vehicles = [EnemyVehicle]()
So I can add elements to this array like this:
vehicles.append(EnemyVehicle(imageNamed:"mySprite"))
And the superclass SKSpriteNode contains an attribute which is position.y, and I can access it in every element of my array with vehicle[x].position.y.
My question is: is it possible to test every specific attribute in an array (here position.y) and watch if it match the conditions? For example, if I want a condition which is true only when all of the position.y values of all my elements in my array are superior than 0? I know I could do it with a loop, but is there another easier way to do it?
Sounds like you want reduce.
Reduce takes a starting value, and then a closure that takes the running value and the next element of the array, and combines these two elements together, carrying that result forward to combine with the next element.
For example:
let a = [1,2,3]
// starting with zero, add each element to a total
let sum = a.reduce(0) { total, i in total + i }
// sum = 6
To check if all elements in an array match a given criteria, you want a combining function that checks the property and returns true or false, anding that with the previous criteria:
// assuming you’re ok for true for an empty array:
let allAboveGround = vehicles.reduce(true) {
previous, vehicle in
previous && vehicle.position.y > 0
}
There’s one downside to this – as soon as you hit a value that is false, you could stop right there because the overall value cannot be true. Reduce doesn’t cater for this so you may want to write a loop, or maybe an all function that wraps a loop, that quits early under these conditions:
func all<S: SequenceType>(source: S, pred: S.Generator.Element->Bool) -> Bool {
for x in source {
if !pred(x) { return false }
}
return true
}
let allAboveGround = all(vehicles) { $0.position.y > 0 }

Resources