Retrieve object values based on a given list of keys using Javascript

Let's imagine we have an object as follows:

data = {
    Apple: 5,
    Banana: 8,
    Orange: 12,
    Mango: 3
}

If I wanted to retrieve the values corresponding to an array of keys using vanilla JavaScript or jQuery, how could I accomplish this? For example:

var keys = ["Banana", "Mango"]
Object.values(data)[keys]

The desired output would be an array containing the values [8, 3]

Answer №1

To achieve this, you can utilize the map function

const info = {
    apple: 5,
    banana: 2,
    orange: 8,
    pear: 3
}

let selection = ["apple", "pear"]

const outcome = selection.map(item => info[item])

console.log(outcome)

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

How can I use the JQuery .load() method to send a POST request after a successful ajax post?

Following a successful Ajax post, I am looking to render the template associated with POST using JQuery's .load() method in the handler function. Every time a successful POST is made, the GET request keeps getting called, resulting in the rendering of ...

Check if the input value was chosen by pressing Enter instead of clicking on it

There is an input tag with the following attributes: <input type="text" name="cOperator" class="form-control scale-input operator" placeholder="Enter your ID" autocomplete="off" onkeyup="ajax_showOptions(this,'getEmp',event)" required> ...

Bootstrap is not being rendered by Express

As I venture into learning Express, I've come across an issue regarding the rendering of Bootstrap fonts in my application. Despite setting up the correct paths, the font appears as simple Times New Roman instead of the desired Bootstrap font when run ...

Get a selection of items from a shuffled array in a random order

How can I use Powershell v4 to read a file containing SKUs and product names, randomize the entries, and display a random selection of SKUs and names? The file may contain up to twenty thousand entries, but I only need to show between one and fifty at a ti ...

Exploring the Power of JSONObject and JSONArray in Jmeter

Having an issue with constructing a Json for Amazon Kinesis. The Json needs to follow this format: { "Records": [ { "Data": "XzxkYXRhPl8x", "PartitionKey": "partitionKey1" }, { "Data": "f1PxF ...

An IndexError occurs when attempting to remove both the initial and final elements from a Numpy array

I am currently working on a math program for my mathematics class that involves generating an array of numbers from 1 to 10. My objective is to remove the number 10s and then eliminate pairs of numbers in the array that add up to at least 10. To accomplish ...

Notification does not appear once the full content of all iframes on the page has been loaded

<!DOCTYPE html> <html lang="en"> <head> <title> </title> <meta charset="utf-8" /> <link rel="stylesheet" type="text/css" href="css/custom.css" /> </head> <bo ...

Prevent scrolling through a fixed, scrollable div using the mousewheel

I am encountering an issue on my webpage where I have a fixed y scrollable sidenav and a main body. The problem arises when scrolling with the mouse wheel while the pointer is inside the side nav, causing the body to scroll depending on the element's ...

When utilizing an object within another object for a select option in Vue.js, the value may not update as expected

I'm currently working with an array of posts that includes a user for each post. I have a select field where I can change the user associated with a post. However, only the user's id updates on the page and not their username. Is there a way to e ...

What is the best way to monitor a document variable that is set after a component has been

Is there a way to access the variable document.googleAnalytics, added by Google Tag Manager, for A/B testing on a Vue.js page? I attempted to initialize the variable in both the mounted and created methods of the component, but it returns as undefined: ex ...

Serialization of objects in Jackson using Java without specifying a fixed type

I need to parse the string {"a": 1.0} into a generic Java Object while preserving its original format. However, my attempts using Jackson result in changing the internal representation to {a = 1}. How can I ensure that the code below prints {"a": 1.0} ra ...

What is the most efficient method for exporting React components in an array format?

I've successfully implemented this code, but I'm looking for ways to enhance its clarity. I am in the process of building a compilation of SVG patterns and aim to design a dashboard that showcases each pattern similar to what you would find on t ...

Issue with Jquery similar to javascript createElement

I am attempting to replicate the code below using jQuery: var newElem = document.createElement("div"); newElem.innerHTML = "DynaColumn"; newElem.className = "ui-state-default ui-corner-all"; return newElem; This is the jQ ...

Mastering Promises, Async/Await for managing the flow of execution

Exploring the concepts of promises, await, and async functions has been a fascinating journey for me. As I delved into promises, I discovered an interesting aspect - the unpredictable order in which requests were resolved when multiple requests were sent o ...

Is it possible to utilize a variable from a Higher Order Function within a different generation function?

I am facing a dilemma with the need to utilize email = user.email in newcomment['comments/'+id] = {id,comment,email,date}. However, I am unable to incorporate email = yield user.email or yield auth.onAuthStateChanged(user => {email = user.em ...

Unable to navigate through images on Bootstrap Carousel using previous and next buttons

Despite following tutorials and examining the HTML code on bootstrap, I'm facing issues while trying to create a carousel. I believed that everything was done correctly, but when I click on the next button, nothing happens. <!DOCTYPE html> < ...

Utilize JSX components within the strings file

How can I integrate jsx components into a strings.js file in React and React Native? Storing copy-text for an app in a strings.js file is effective for regular text. However, when the text includes other React components, it poses a challenge. For instan ...

Trouble populating array using the .Split method

I am struggling to populate the array "whyWontYouWork" with a value. In the provided example, the value of rangeNames[j] is "$A$1:$A$10". Although the string "group" fills in correctly as "$A$1:$A$10", there is an error message stating, "The name 'why ...

The retrieval of a single item from a Firestore database using a POST request is experiencing issues

I have set up a Firestore application which currently has a collection named 'students' with 5 documents. One of those documents is assigned the id: 1 Using an HTML form, I am able to add individual students to this collection. Additionally, I&a ...

Error: Unable to locate module 'child_process' in the context of NextJS + Nightmare

Encountering an issue while trying to compile a basic example using Next JS + Nightmare Scraper. Upon attempting to access the page, I am faced with the following error message, and the page fails to load. PS C:\Users\lucas\Documents\Pr ...