我有一系列数字,我需要确保它们是唯一的.我在互联网上找到了下面的代码片段,它的工作情况很好,直到数组中的数字为零.我发现这个其他脚本在SO上看起来几乎就像它,但它不会失败.
所以为了帮助我学习,有人可以帮我确定原型脚本出错的地方吗?
Array.prototype.getUnique = function() {
var o = {}, a = [], i, e;
for (i = 0; e = this[i]; i++) {o[e] = 1};
for (e in o) {a.push (e)};
return a;
}
Run Code Online (Sandbox Code Playgroud)
如何获取数组中的唯一值列表?我是否总是必须使用第二个数组,或者在JavaScript中是否有与java的hashmap类似的东西?
我将只使用JavaScript和jQuery.不能使用其他库.
这里我提到了当前的图像形式。

1) 它是一个多选下拉菜单。它应该限制重复的税名。对于此图像中CGST选择的两次示例。
选择相同的名称时,它不应显示在此处。所以请帮助我做到这一点。
html
<div class="form-group">
<label for="tax">Tax Name</label>
<select class="form-control" id="tax_name" (change)=" dropdown(tax.value)" [(ngModel)]="model.tax" name="tax_name" #tax="ngModel">
<option value="addNew">
<i class="fa fa-plus" aria-hidden="true"></i>Add New Tax </option>
<hr>
<br/>
<option *ngFor="let i of taxlist" [value]="i.tax_name">{{i.tax_name}} ( {{i.tax_percentage}}% )</option>
</select>
</div>
<div>
<label for="percentage">Tax Percentage</label>
<input type="text" class="form-control" id="tax_percentage" placeholder="Percentage" pattern="[0-9]+" minlength="0" maxlength="3"
[(ngModel)]="per" name="tax_percentage" #percentage="ngModel">
</div>
<button (click)="addContact(tax.value,percentage.value);" type="button" class="btn btn-success btn-sm text-right">
<i class="fa fa-plus fa-lg"></i>
</button>
Run Code Online (Sandbox Code Playgroud) angular2-forms angular2-directives angular2-template angular