I have one dictionary which is stored in field_detail
<li ng-repeat = "field in field_detail">{{field.displayName}}</li>
Now I dont want to include duplicate displayName
of field_detail
, what filter
should I use?
I have one dictionary which is stored in field_detail
<li ng-repeat = "field in field_detail">{{field.displayName}}</li>
Now I dont want to include duplicate displayName
of field_detail
, what filter
should I use?
Just create a filter that gets the unique values, probably by a key. Something like this ought to do (I didn't test this at all, so I'll leave that business to you, this is just to give you an idea):
Note: Array.indexOf() does not work in IE8, so if you're supporting IE8, you'll need to stub in indexOf(), or you'll need to use a slightly different approach.
Other thoughts: It's probably better just to create a filter that leverages the unique function in lowdash or underscore if you're already referencing those libraries.
I get a ticket saying when the user clicks save they get an error saying Unique Constraint violation, blah blah.. They want my screen to show them duplicates before the database enforces the constraint. So I wrote a function to loop through all instances of the array of objects and count the instances of it.
...and I use this function in my script to prevent the save, like this:
...and to show the user as soon as they add a new record, like this:
... and then in my HTML I have something to tell the user where the duplicate is...
Yes, I'm intentionally comparing each instance to itself. There's less code that way. I check to see if .cnt > 1 instead of > 0.
Also to take note of... the kpiList.cnt field doesn't exist until the function runs for the first time. The ng-show="row.cnt > 1" will indicate a false (not truthy) and will not display until row.cnt has a value.
Also, you should use a StyleSheet to format your span instead of putting it in the style attribute.
Create your own function:
});
To piggyback off of Ben Lesh, I added the option to remove duplicate objects in an array if the keyname is falsy:
I worked up a JSFiddle based on the answer provided by Ben Leash:
http://jsfiddle.net/ThunderHemlock/bvsvzrr5/1/
Thanks, Ben. Other answers required the use of AngularJS UI or other such additional frameworks.