What is the method for displaying cities from a JavaScript array using zip codes?

Hey there! I'm currently working on a feature that involves taking user input for zip codes and triggering the "key up" event to populate available stores and cities in that area. Below is a snippet of the code I've been using:

delivery.component.ts

zip: any = {
  "10020": ["A", "B" ],
  "20050":[ "X", "Y" ]
};

getCityFunction = (theCurrentZip: any) => {
  let key = Object.keys(this.zip).filter(z => {
    return z.includes(theCurrentZip)
  }).values();
 
console.log("here",key);
 
}



onKeyUpTrigger(event: any){
    this.deliveryForm.patchValue({
      city: this.getCityFunction(event.target.value)
    })
  }

delivery.component.html

<form [formGroup]="deliveryForm" >
<div style="margin: 0 auto;text-align: left;">
      <div>
          <label>Zip Code:</label>
          <div><input id="zip" type="text" formControlName="zip" (keyup)="onKeyUpTrigger($event)"/></div>
      </div>
      <div>
        <label>City Name:</label>
      <input id="city" type="text" formControlName="city">
      </div>
  </div>
</form>

Answer №1

obtainCity = (selectedZip: any) => {
  return this.zip[selectedZip];    
}

I believe this function should successfully retrieve the city based on the provided zip code.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is it possible to utilize a variable for binding, incorporate it in a condition, and then return the variable, all while

There are times when I bind a variable, use it to check a condition, and then return it based on the result. const val = getAttribute(svgEl, "fill"); if (val) { return convertColorToTgml(val); } const ancestorVal = svgAncestorValue(svgEl, "fill"); if (a ...

Creating an AJAX function to display a popup window for users who are already registered - here's how!

I am currently working on a dropwizard-java project. Whenever users click the subscribe button, it displays a thank you message for subscribing and then checks if the user is already registered. I would like to have a pop-up window immediately show whethe ...

Create a JavaScript array by extracting IDs from an HTML document

Looking to create a function that randomly selects an image defined in HTML code. I've opted to create an array to store all used images and have the function pick from there. Is there a simpler way to define the array or should I skip using it altog ...

Use JavaScript and Handlebars to compile any templates that have not yet been compiled

I have an HTML file containing various templates that I want to compile when the page loads. I'm looking for a way to either compile and store all templates in an array upfront, or compile templates on the fly as users navigate through the SPA. I am u ...

Ways to enable a file download to a user's computer when a specific HTML button is clicked

<button type="submit" class="myButton" onclick="window.open('test.txt')">Cash Machine</button> This code is not functioning as expected when clicked. The desired action is to prompt the download of test. ...

Exploring the benefits of leveraging TypeScript with AWS NodeJS for improved stacktrace visibility over traditional JavaScript

I'm contemplating the idea of transitioning my existing JavaScript codebase to incorporate TypeScript in NodeJS. One aspect that I am concerned about is being able to view the stack trace in AWS CloudWatch (request log) in case an error occurs during ...

Is it possible to recognize when the mouse button is held down and the cursor is outside the viewport by using mouseleave detection?

Is there a way to detect when a user moves the mouse outside of the view-port, even if they are holding down the mouse button (for example, if the mouse is on the browser address bar)? In the code below, I am currently using mouseout and mouseleave to det ...

Connect an HTML button to a Java activity

I am currently developing an Android app for my book and I have created an HTML page. I would like to add a button on this page that is linked to the following Java code: public class MainActivity extends Activity { @Override public void onCreate(Bundle ...

Tips for incorporating a visible marker beside the dropdown arrow

When developing my React application with React hooks forms, I have a select component where I need to include a clear indicator 'x' icon next to the dropdown icon. The current select component code is: <Form.Control size="sm" as=&q ...

Dealing with a socket.io CORS problem

After deciding to implement a websocket on my website, I opted to utilize the socket.io library. However, I've encountered a CORS error: Access to XMLHttpRequest at 'http://localhost:2021/socket.io/?EIO=4&transport=polling&t=NlbFGS2&apos ...

Exploring Vue and Nuxt JS: What Causes the Issue of Unable to Create the Property 'display' on the String 'bottom:30px;right:30px;'

this piece of code is designed for a component that allows users to jump back to the top of the page. However, after refreshing the page, it stops working and throws an error. The project uses the Nuxt and Vue framework. Can anyone identify the reason behi ...

Laravel - AutoComplete Feature: Populate Input Field with JSON Data Instead of Suggestions

I've been working on adding search functionality to my Laravel 5.4 project, but I'm running into an issue. The suggestions show up fine in the dropdown menu, but when I select one, the input field gets filled with JSON data instead of the suggest ...

What is the best way to deal with a "Access to restricted URI denied" error in JavaScript while utilizing XMLHttpRequest?

Here is some code I am working with: var req = new XMLHttpRequest(); req.onload = function(){ if (req.status === "200"){ doSomethingWithTheReceivedData(); } else { alert("Error msg"); } }; When running index.html directly ...

Issue with Kendo dropdown's optionLabel functionality malfunctioning

Check out the Kendo code snippet below for a dropdown control. I'm currently facing an issue where I am trying to display a "Please select" option in the dropdown. This code works perfectly fine for all other dropdowns except for this specific one. T ...

Tutorial on making a pop-up appear on click using jQuery

Below are the attempts I have made: Using jQuery: $("#type_name").click(function(){ $("#add_form").fadeIn(1000); $("#involved_name").val(""); positionPopup('#add_form'); $("#involved_name").focus(); }); Usin ...

Ways to ensure that v-model does not become "true" or "false" in an input checkbox using Vue

I am currently working on a filter popup that includes a list of checkboxes. By default, some items should be selected and others not selected. I have connected these checkboxes to an object array using v-model. My issue is that when I deselect and select ...

"The distinctive sound of an Internet Explorer click paired with the power

Is it possible to prevent the click sound in Internet Explorer that occurs when submitting a form using jQuery? ...

Ensure selected language is maintained when refreshing or changing view by utilizing switch i18n functionality

Hello there, I am facing a challenge with "JavaScript Localization" on my website. The issue is that I cannot figure out how to prevent the DOM from prioritizing the local language of the browser and instead use the language set in the switch as a referenc ...

Guide: "Sending an AJAX request upon selecting an item in a Vue.js 'Select' element using vue-resource"

Is it possible to send an ajax request through vue-resource when a specific item is selected from a "Select" in vuejs? Check out the demo here: https://jsfiddle.net/xoyhdaxy/ <div class="container" id="app"> <select v-model="selected"> ...

How can the button press, release, drag, and drop events be implemented in Ionic?

I have been working on adding a recording feature that allows for recording when a button is pressed and stops when it is released. I decided to use the ionic-long-press plugin for this functionality. However, I have noticed that it does not support drag ...