Use filter() to remove values below zero and then check if the length of resulting array is greater than or equal to two
const twoGreaterThanZero = arr => arr.filter(x => x > 0).length >= 2;
console.log(twoGreaterThanZero([9, 1, 0])) //true
console.log(twoGreaterThanZero([0, 0, 0])) //false
console.log(twoGreaterThanZero([5, 0, 0])) //false
Answer from Maheer Ali on Stack OverflowUse filter() to remove values below zero and then check if the length of resulting array is greater than or equal to two
const twoGreaterThanZero = arr => arr.filter(x => x > 0).length >= 2;
console.log(twoGreaterThanZero([9, 1, 0])) //true
console.log(twoGreaterThanZero([0, 0, 0])) //false
console.log(twoGreaterThanZero([5, 0, 0])) //false
To avoid wasted effort, you should stop the checking as soon as the condition is met. I think this meets your requirement.
function twoGreaterThanZero(arr) {
let counter = 0
for(let x of arr) {
if(x > 0 && (++counter > 1)) return true
}
return false
}
const a = [9, 1, 0]
const b = [0, 0, 0]
const c = [5, 0, 0]
console.log(twoGreaterThanZero(a)) // print true
console.log(twoGreaterThanZero(b)) // print false
console.log(twoGreaterThanZero(c)) // print false
check how many arrays have length greater than 0 in javascript and angularjs - Stack Overflow
javascript - array.length vs. array.length > 0 - Stack Overflow
Could array.length be below 0 in Javascript? - Stack Overflow
javascript - How to check if array is empty or does not exist? - Stack Overflow
forEach ignores the return value and executes for all the elements. So it would be both easier and more performant to use some() (docs here) instead, e.g.
const exists = this.Sort.some(s => s.of === 12.2 && s.groups.length > 0);
If you want to use forEach anyway, you could place your boolean value outside of the loop with initial value of false, and then set it's value to true only if the element exists, e.g.
let exists = false;
this.Sort.forEach(e => {
if (e.of === 12.2 && e.groups.length > 0) {
exists = true;
}
});
you can try like this
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
Sort = [
{
id: 1,
of: 12.2,
tam: 's',
name: 'Color Run',
groups: [
{
type: 'type1',
quant: 21
}
]
},
{
id: 2,
of: 12.2,
tam: 'M',
name: 'Color Run',
groups: []
},
{
id: 3,
of: 2.2,
tam: 'L',
name: 'Works',
groups: []
}
];
ngOnInit() {
console.log(this.Sort);
let groupLengthList = [];
for (const data of this.Sort) {
if (data.of === 12.2) {
if (data.groups.length > 0) {
groupLengthList.push(true);
} else {
groupLengthList.push(false);
}
}
}
console.log(groupLengthList);
}
}
You can iterate through the object:
var count = 0;
for (var group in $scope.selected) {
if ($scope.selected.hasOwnProperty(group) && group.values.length > 0)
count++;
}
if (count > 1)
console.log("More than one group with values");
You can use Array#filter and Object.keys(o)
const selected = {
group1: {
values: [{cat: 1, id: 1},{cat:1, id:3}]
},
group2: {
values: [{cat: 2, id: 5},{cat:2, id:2}]
}
};
const validSelected = {
group1: {
values: [{cat: 1, id: 1},{cat:1, id:3}]
},
group2: {
values: []
}
};
function validate(obj){
return Object.keys(obj).filter(k=>obj[k].values.length>0).length === 1;
}
console.log(validate(selected)); //false
console.log(validate(validSelected)); //true
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Without ES6 arrow :
Object.keys(obj).filter(function(k){return obj[k].values.length>0).length === 1});
Is there any difference between checking an array's length as a truthy value vs checking that it's > 0?
Since the value of arr.length can only be 0 or larger and since 0 is the only number that evaluates to false, there is no difference.
In general, Boolean(n) and Boolean(n > 0) yield different results for n < 0.
In other words is there any reason to use one of these statements over the other
Only reasons related to code readability and understanding, not behavior.
array.length is fastest and shorter than array.length > 0. You can see difference of their speeds : http://jsperf.com/test-of-array-length
if(array.length){...} is similar to if(0){...} or if(false){...}