1

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.

enter image description here

TS

checkValues(values) {
    const result = Object.values(values).every((value) => value[1].cuesData.length > 0);
    return result;
}

HTML

<div *ngIf="checkValues(values) === true">

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": []
        }
      ]
]
6
  • You could do the check in a function or component property and reference that property from your ngIf. Commented May 12, 2020 at 7:18
  • 1
    what is the error you are facing Commented May 12, 2020 at 7:19
  • 1
    cuesData is property of object not any array element.*ngIf="values[0][1].cuesData.length > 0" Commented May 12, 2020 at 7:19
  • @ShlokNangia. It should check every array not just the first array. Every array has a cuesData array, so i need to check if at least one cuesData has a value or a length greater than 0 Commented May 12, 2020 at 7:28
  • @Joseph, Do you mean like this? stackblitz.com/edit/my-angular-starter-yiye8g Commented May 12, 2020 at 7:36

2 Answers 2

2

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

Here .every() method will check that all conditions should met but whereas some() method works that at least one condition has been true..

Stackblitz without cuesdata length: https://stackblitz.com/edit/my-angular-starter-cfpxa5

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

3 Comments

Is this not an expensive operation in template?
@Joseph, This is not an operation in template side and you are handling the logic in .ts file only so this is best approach only.. It is always better to handle the logics in typescript file..
@Joseph, Also added another example that has another scenario of not having any cues data values in any of the array.. Hope this helps and I hope that this solution won't cause any performance issue..
0

You can use some method for this:

*ngIf="CheckValues(values)"

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

3 Comments

Is this not an expensive operation in template?
yes it is, if you want, you can check this when you get your data and set any variable for ng if
My data is changing always.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.