0

This is angularjs app. In a view I have access to an array of objects:

var arrayOfObjects = [{"name":"blue"},{"name":"red"}];

I have then a div that should display only if the arrayOfObjects contains an entry with

name=='red' 

I had a look at "contains" but seems to work only on arrays of elements, not of objects. Is it possibile to do this directly in the view without needing to code this in the controller?

1 Answer 1

1

Since the array does not understand what is inside of it (in this case object), you have to check each element in the condition.

A short-way to do that, would be using the some method like this:

<div *ngIf="arrayOfObjects.some(({ name }) => name == 'red')"></div>

The some will return true if some element in arrayOfObjects satisfy this condition. Other way, would be to map the arrayOfObjetcs to arrayOfNames and them check if that array contains name, like this: arrayOfObjects.map(({ name }) => name).contains('red')

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.