[英]Check If Array Has Value in Angular
I have to check if any of the "cuesData" has a value or length greater than 0. In my code below, i can only check the first array but not the others.我必须检查是否有任何“cuesData”的值或长度大于 0。在下面的代码中,我只能检查第一个数组,而不能检查其他数组。
TS TS
checkValues(values) {
const result = Object.values(values).every((value) => value[1].cuesData.length > 0);
return result;
}
HTML HTML
<div *ngIf="checkValues(values) === true">
JSON JSON
[
[
"videoData__1",
{
"id": 1,
"title": "Pale Blue Dot",
"stoppedAt": 97.834667,
"cuesData": [
{
"startTime": 25.335678,
"endTime": 35.335678,
"description": "fqff"
}
]
}
],
[
"videoData__2",
{
"id": 2,
"title": "Big Buck Bunny",
"stoppedAt": 247.57881,
"cuesData": []
}
],
[
"videoData__3",
{
"id": 3,
"title": "Elephants Dream",
"stoppedAt": 404.585327,
"cuesData": []
}
]
]
Change,改变,
checkValues(values) {
const result = Object.values(values).every((value) => value[1].cuesData.length > 0);
return result;
}
To至
checkValues(values){
const result = Object.values(values).some((value) => value[1].cuesData.length > 0);
return result;
}
Working Stackblitz: https://stackblitz.com/edit/my-angular-starter-j4yypu工作 Stackblitz: https://stackblitz.com/edit/my-angular-starter-j4yypu
Here .every()
method will check that all conditions should met but whereas some() method works that at least one condition has been true..此处.every()
方法将检查是否应满足所有条件,但some()方法工作时至少有一个条件为真。
Stackblitz without cuesdata length: https://stackblitz.com/edit/my-angular-starter-cfpxa5 Stackblitz 没有提示数据长度: https://stackblitz.com/edit/my-angular-starter-cfpxa5
You can use some method for this:您可以为此使用一些方法:
*ngIf="CheckValues(values)"
function: function:
CheckValues(values : any[]){
return values.some(v=>v[1].cuesData&&v[1].cuesData.length); //if any array has cuesData, some will return true
}
more about some
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some更多关于some
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.