Find and filter the values in the range.getValues() array that correspond to the first element in apps script

In the spreadsheet provided, I have compiled a list of cities in different states of my country. This information will be utilized in a form; however, it is not feasible for users to sift through an extensive list of all cities listed. Therefore, I am looking to implement a filter system based on the state.

Due to the large logging output, it has been truncated. [[AC, Acrelândia], [AC, Assis Brasil], 
[AC, Brasiléia]...

The initial element in each entry signifies the state, and I intend to refine this array so that only values belonging to the chosen state are displayed. For instance, if the selected state is AC, the result would be:

[[AC, Acrelândia], [AC, Assis Brasil], [AC, Brasiléia]...

Answer №1

Explanation

If you want to retrieve all the cities located in a specific state, you can easily achieve this by using Array.filter(). In this example, "AC" is hardcoded into the filter function, but you could substitute it with a variable for flexibility. The dataset has been purposely shuffled to demonstrate that only the cities in AC are being extracted.

Code Snippet

function test() {
  try {
    let data = [["AC", "Acrelândia"], ["AC", "Assis Brasil"], ["AC", "Brasiléia"], [...]
    let ac = data.filter( row => row[0] === "AC" );
    console.log(ac); 
  }
  catch(err) {
    console.log(err);
  }
}

Output of Console.log

7:56:24 AM  Notice  Execution started
7:56:26 AM  Info    [ [ 'AC', 'Acrelândia' ], [...]
7:56:25 AM  Notice  Execution completed

Additional Resources

Answer №2

Best Cities to Visit in Each State

function findTopCitiesForState(state = "CA") {
  const cities = [[],[]];//add your favorite cities here
  Logger.log(cities.filter(e => e[0] == state).map(r => r[1]));
  return cities.filter(e => e[0] == state).map(r => r[1]);
}

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

Determine total number of covid-19 cases with JavaScript

I am looking to incorporate a real-time COVID-19 total cases tracker for Russia on my website. In order to achieve this, I am sending a request to using the following script: function httpGet(theUrl) { var xmlHttp = new XMLHttpRequest(); xmlHttp.o ...

Leveraging Async/Await to track error counts across three distinct loops, each invoking an asynchronous function in every iteration

While I have experience with Callbacks, Async/Await and Promises are new concepts to me. In my node.JS server project, I am faced with the challenge of counting errors generated by thousands of asynchronous calls from three different async functions. My g ...

How can I accommodate pushState routes from Backbone.js on a node.js express server?

pushState feature was added to Backbone.js in version 0.5. According to the backbone documentation: When using real URLs, your web server must be capable of rendering those pages as well. For example, if you have a route like /documents/100, your web s ...

Disregard Cloudflare's Automatic RocketLoader feature for certain JavaScript scripts

After extensive research and failed attempts, I am seeking help to disable Cloudflare Rocketloader for a specific JavaScript file on my WordPress website. Specifically, I need to exclude the Automatic Rocket Loader for a particular .js file. I attempted t ...

sending an alert via a function's return statement

Could you explain to me why this code isn't functioning properly? When I call s.A, the alert message doesn't appear. Can you help me understand why? var s = { A: function () { alert("test A"); }, B: function () { alert("test B"); } }; ...

Is it possible to delete an element from both local storage and HTML by referencing its ID?

Experience a simple flashcard game where you enter a question and answer to create a new flash card stored as an object within the cards array. The newly created flash card is also displayed by appending a new element to the flash cards section on the webp ...

(Processing) Eliminating Previously Utilized Element from Array

Apologies for the poorly worded title. I'm working on a "War" game in Processing for my Programming course. I need help modifying my code to remove each used card from the deck/array. I've come across some references to "ArrayList" in online post ...

Manipulate md-select options with Angular using setValue

My goal is to update a select option, but I'm having trouble getting it to work properly. When I use this.eventForm.controls.venue.setValue(event.venue.name);, the value of venue changes to match event.venue.name. However, the select option itself doe ...

Is there a way to prevent the background color from filling the entire container?

In the visual representation provided below, there is a header element consisting of a back arrow and a name. The arrow container has been assigned flex: 1, while the arrow and name containers have been set to flex-start and flex-end respectively. This co ...

Trouble with data binding in AngularJS when using the input element

I am encountering an issue with my AngularJS code. When I try to input text into the textbox, it is not appearing in the binding. <!DOCTYPE html> <html lang="en"> <head> <script src="js/angular.js"></script> <scr ...

I am currently working on a project using ar.js

I came across a glitch code that triggers a video when scanning the marker. However, I encountered an issue where it only works on desktop. On Chrome for Android, the video does not appear, only the sound can be heard. I need assistance as my coding knowle ...

Calculate the product of a JavaScript variable and a PHP variable, then show the result on the

I need to calculate the product of a PHP variable value and a JavaScript variable, then show the result on the screen. Here is my PHP code: <?php require 'config.php'; session_start(); ?> <html> <head> < ...

Error: The variable "Set" cannot be found in the react.js code, specifically in Safari browser

An issue has been identified in Safari where a ReferenceError is thrown: "Can't find variable: Set" when using react.js. This error occurs only in Safari, while all other browsers work perfectly fine. The error message points to the main.js file which ...

Creating a hierarchical list structure from a one-dimensional list using parent and child relationships in JavaScript

I am in the process of developing a web application that requires handling nested geographical data for display in a treeview and search functionality. The initial raw data structure resembles this: id:1, name:UK id:2: name: South-East, parentId: 1 id:3: ...

Display the names of files depending on the selection made in the drop-down menu

In order to utilize a value from a select box for a PHP function later on the page, I am seeking assistance. Within my index.php file, there is a standard select drop down containing folder names as values and options. After selecting a folder, I aim to e ...

The jQuery functions seem to be malfunctioning when trying to retrieve elements by their ids

I'm having trouble with the click function when using IDs, but it works fine with classes. $('#myTab li a').click(function(e) {} When I switch to using classes like this: $('.nav-tabs a.overview').click(function(e) {} it work ...

What is the method for adding a prefix to every line in JavaScript?

I'm currently working on a React project and dealing with a string that may contain newlines. I'm looking for the most efficient way to inject a symbol at the start of each line. I've considered exploding the string into an array and adding ...

What is the most effective way to showcase a list of image URLs in HTML with Vue?

Currently, I am working with an array called thumbnails that holds the paths to various images. My goal is to use masonry in Vue to arrange these images in a grid format, but I'm encountering some difficulties achieving the desired outcome. This is m ...

Transform the file format from .casa-model to .obj

Looking for a way to convert a file with the extension .casa-model to .obj format. Is there any solution available? Find the model here. ...

Exploring Next.js' dynamic routes with an alternative URL approach

Currently in the process of transitioning a React project to Next.js, I've encountered a minor issue with Dynamic Routing that doesn't seem to have any readily available solutions online. I have multiple information pages that utilize the same c ...