Exploring an array to retrieve a specific value

Can someone assist me?

I am looking to create a JavaScript function that follows the structure of the code example below. Unfortunately, I do not have enough programming skills to develop a functional solution from scratch.

The goal is to input a value, search through an array using that value, and return the corresponding short name (the value on the right side of the colon character).

function findShortName() {

var filenames = {
        "REQUEST FOR INFO": "REQI",
        "MEDIA CALL": "MC",
        "ISSUES NOTE": "ISN"
    };

EX1.)

    var value_to_search_for = "REQUEST FOR INFO (ALPHA)";

    if (filenames[value_to_search_for]) {

        return filenames[value_to_search_for];

    }

EX2.)
    var value_to_search_for = "MEDIA CALL";

    if (filenames[value_to_search_for]) {

        return filenames[value_to_search_for];

    }

}

Answer №1

You have the option to convert it into an object and proceed with this approach

var filenames = {
  "REQUEST FOR INFO": "REQI",
  "MEDIA CALL": "MC",
  "ISSUES NOTE": "ISN"
};

var getValue = function(val, obj) {
  if (val in obj) return obj[val];
}

console.log(getValue('ISSUES NOTE', filenames));

You can also opt for an array of objects and then you can perform this action

var filenames = [
  {"REQUEST FOR INFO": "REQI"},
  {"MEDIA CALL": "MC"},
  {"ISSUES NOTE": "ISN"}
];

var getValue = function(val, array) {
  array.forEach(function(el) {
    for (prop in el) {
      if (prop == val) console.log(el[prop]);
      }
    });
  }

getValue('MEDIA CALL', filenames);

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

Tips for improving the efficiency of a Solidity smart contract function that removes an element at a specific index within an array

Seeking a more efficient solution, I utilized the following library code to remove an element from an array based on its index. While my current implementation involves using a loop, I am concerned about the high gas costs associated with this approach. ...

What is preventing window.scrollTo() from being executed?

I have implemented Bootstrap's Buttons plugin to toggle the state of a custom checkbox. When the button is toggled on, a hidden element should appear at the top of the page and then the page should scroll to the top. Similarly, when the button is togg ...

How does the single-threaded nature of Node.js handle an abundance of concurrent requests?

I'm currently delving into the world of nodejs, trying to wrap my head around its single-threaded nature. Here's a pondering I have: Let's say I implement a non-blocking method and we have 20000 concurrent requests flowing in. If one request ...

Getting started with JavaScript arguments can be overwhelming for newcomers

Can you figure out why the variable 'a' doesn't increase by 1 in the following code snippet? var a = 5; function abc(y) { y++; } abc(a); // The value of 'a' remains 5, instead of increasing to 6. Why is that? However, when ...

MUI: Autocomplete received an invalid value. None of the options correspond to the value of `0`

Currently, I am utilizing the MUI autocomplete feature in conjunction with react-hook-form. I have meticulously followed the guidance provided in this insightful response. ControlledAutoComplete.jsx import { Autocomplete, TextField } from "@mui/mater ...

Using specific delimiters in Vue.js components when integrating with Django and Vue-loader

While working on my Django + Vue.js v3 app, I came across a helpful tool called vue3-sfc-loader. This allows me to easily render .vue files using Django, giving me the best of both worlds. The current setup allows Django to successfully render the .vue fil ...

Tips for altering the appearance of the material-ui slider thumb design when it is in a disabled state

Through the use of withStyles, I have successfully customized the style of the Slider: const CustomSlider = withStyles(theme => ({ disabled: { color: theme.palette.primary.main }, thumb: { height: 24, width: 24, }, }))(Slider); How ...

What is the best way to assign multiple values to certain elements within an array?

I have an array that looks like this: items = { item1: 10, item2: 5, item3: 7, item4: 3, }; I am looking to include additional details in this array, for example: items = { item1: 10 {available: true}, ...

Exploring the Ins and Outs of Debugging JavaScript in Visual Studio Using

I encountered a peculiar issue while testing some code. When the program is executed without any breakpoints, it runs smoothly. However, if I introduce a breakpoint, it halts at a certain point in the JSON data and does not allow me to single-step through ...

Learn the process of transmitting data from middleware to components and APIs in Next.js version 13

I've been experimenting with the Next Js 13 middleware feature and I'm a bit confused about how to pass data from the middleware to components/pages/api. For example, when trying to pass payload data or determine who the currently logged-in user ...

Uploading files seamlessly without the need for refreshing the page

On my HTML page, I have a form input that allows users to upload a file. Here is the code snippet: <form action = "UploadFile.jsp" method = "post" target="my-iframe" enctype = "multipart/form-data"> <input type = "file" name = "file" ...

Dealing with hefty JSON documents using iron-ajax, iron-list, and loading with iron-scroll-threshold

I'm facing an issue while attempting to load a large JSON file into my view that contains over 1000 items. My goal is to have the view import only 20 items at a time. However, every time I utilize iron-list, it only renders 3 items initially until I r ...

Send the user to an Angular route once they have successfully authenticated with Google

I'm facing an issue with redirecting users to an Angular route. Here's the scenario: When I'm on the login page and click on Google login, I get redirected to Google for authentication. After successfully logging in, I want to be redirecte ...

Bringing @angular/code into a directory that is not within an Angular project

Currently, I have an Angular 2 project folder with a separate service folder named "auth.service.ts" located outside of it. Within this service file, I am importing `Injectable` from `@angular/core`. However, due to the service being located outside of t ...

Leverage the JSON data to populate the category axis in c3.js

I have a JSON object that contains an array called datas, which holds the data I need to use for my chart. I want to utilize the data1 array for the x-axis in a category format. How can I achieve this? This is my attempted solution, where I extract the da ...

Create a roster of numbers that are multiples using a systematic approach

Is the following code a functional way to return multiples of 5? function Mul(start,array,Arr) { Arr[start]=array[start]*5; if(start>array.length-2){ return Arr; } return Mul(start+1,array,Arr); } var numbers =[1,2,3,4,5,6 ...

Javascript - Converting a function to run asynchronously

Just starting to work with Node.js for the first time and feeling a bit puzzled by asynchronous functions. I'm getting better at identifying when async is causing issues, but still unsure how to fix them. Here's the code snippet in question: fu ...

Can the swipe navigation feature be disabled on mobile browsers?

Currently, I am in the process of creating a VueJS form that consists of multiple steps and includes a back button for navigating to the previous step. The steps are all managed within a parent component, which means that the URL and router history remain ...

Insert information into an array within a mongoDB structure using the Mongoose library

Is it possible to add elements into an array in a mongoDB schema? For instance, in the given schema: var ProviderSchema = new Schema({ keyWords: [String] }); How can I insert data into the keyWords field using the specified route: app.put(&a ...

Having trouble with ui-sref functionality within a Bootstrap dropdown menu

In the header.html file, I have the following angular code: <div id="navbar" class="navbar-collapse collapse"> <ul class="nav navbar-nav navbar-right"> &l ...