如何根据 javascript 数组中的邮政编码打印城市?
how to print cities based on zip code in javascript array?
我正在尝试从用户输入中获取邮政编码并触发按键事件,该事件将填充该地区可用的商店/城市,下面是相同的代码片段..
delivery.component.ts
zip: any = {
"10020": ["A", "B" ],
"20050":[ "X", "Y" ]
};
getCity = (theCurrentZip: any) => {
let key = Object.keys(this.zip).filter(z => {
return z.includes(theCurrentZip)
}).values();
console.log("here",key);
}
onKeyUp(event: any){
this.deliveryForm.patchValue({
city: this.getCity2(event.target.value)
})
}
delivery.component.html
<form [formGroup]="deliveryForm" >
<div style="margin: 0 auto;text-align: left;">
<div>
<label>Zipcode:</label>
<div><input id="zip" type="text" formControlName="zip" (keyup)="onKeyUp($event)"/></div>
</div>
<div>
<label>City:</label>
<input id="city" type="text" formControlName="city">
</div>
</div>
</form>
getCity = (theCurrentZip: any) => {
return this.zip[theCurrentZip];
}
如果我没有遗漏任何重要的事情,它应该可以完成工作。
我正在尝试从用户输入中获取邮政编码并触发按键事件,该事件将填充该地区可用的商店/城市,下面是相同的代码片段..
delivery.component.ts
zip: any = {
"10020": ["A", "B" ],
"20050":[ "X", "Y" ]
};
getCity = (theCurrentZip: any) => {
let key = Object.keys(this.zip).filter(z => {
return z.includes(theCurrentZip)
}).values();
console.log("here",key);
}
onKeyUp(event: any){
this.deliveryForm.patchValue({
city: this.getCity2(event.target.value)
})
}
delivery.component.html
<form [formGroup]="deliveryForm" >
<div style="margin: 0 auto;text-align: left;">
<div>
<label>Zipcode:</label>
<div><input id="zip" type="text" formControlName="zip" (keyup)="onKeyUp($event)"/></div>
</div>
<div>
<label>City:</label>
<input id="city" type="text" formControlName="city">
</div>
</div>
</form>
getCity = (theCurrentZip: any) => {
return this.zip[theCurrentZip];
}
如果我没有遗漏任何重要的事情,它应该可以完成工作。