What is the best way to extract an array of values associated with a specific key from a series of objects within an array using JavaScript?

I am looking to extract an array of values associated with a specific key from a collection of objects within an array using JavaScript.

Here is the parent array:

const data = [
  {employeeId: 033, field: "TAX", active: 1},
  {employeeId: 035, field: "ACCOUNTING", active: 1},
  {employeeId: 035, field: "SALES", active: 1}
];

The desired output (an array of values for the key named 'field'):

["TAX", "ACCOUNTING", "SALES"]

Answer №1

If you want to extract specific values from an array in JavaScript, you can utilize the Array#map method.

const data =[{employeeId: 033, field: "TAX", active: 1},
{employeeId: 035, field: "ACCOUNTING", active: 1},
 {employeeId: 035, field: "SALES", active: 1}];
const result = data.map(item => item.field);
// alternatively, you can use a.map(({field})=>field);
console.log(result);

Answer №2

Utilizing array.map can help streamline the process of transforming an array.

const data =[{employeeId: 033, field: "TAX", active: 1},
{employeeId: 035, field: "ACCOUNTING", active: 1},
 {employeeId: 035, field: "SALES", active: 1}];
 
 console.log(data.map((entry) => entry.field));

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

What is the best way to include a character in a string if there are no instances of the same character already present?

Looking for help with a function that can determine if a string contains a period and add one if it doesn't. So far, I'm struggling to make it either continuously add periods or not add any at all. function checkPeriod() { if (inputString.indexO ...

Navigate using history.push with user Logout Icon

Currently, I am utilizing a Material UI icon as a logout button in my project. Here is how I have implemented it: function logout(props:any){ localStorage.removeItem("token"); return( <Redirect to="/login" /> ) //props.history.push("/log ...

An alternative approach to coding

I am a JavaScript developer who writes code to handle multiple keys being pressed simultaneously. The current implementation involves checking each key's keyCode individually, which I find cumbersome. var key = event.keyCode; if (key === 39 { / ...

Looped jQuery setTimeout not functioning correctly

I am facing an issue with processing a JSON file that contains a list of serialized objects. My goal is to walk through this JSON and display the messages from it one at a time, with a delay of 2 seconds between each message before ultimately stopping. I a ...

When JavaScript and HTML combine, they create overlapped audio

I am currently working on a project that aims to assist individuals with visual impairments through a website. The main functionality of the site involves playing audio files using keyboard buttons. However, I have encountered an issue where playing one ...

Manipulating the contents of a C++ C-style string by deleting characters

I have a .txt file with the following content... City- Paris Colour- Blue Food- Croissant Language- French Rating- 5 stars I am trying to separate the text before the hyphen or whitespace into one array and the text after into another array. My current ...

Guide on incorporating a Bootstrap date time picker in an MVC view

Is there a live example available that demonstrates how to implement the latest Bootstrap date time picker in an MVC view? In my project, I have already included: - jQuery. - Bootstrap JS. - Bootstrap CSS. ...

PHP 2D associative array losing modified values

I have encountered a perplexing issue related to PHP and a 2-dimensional associative array. I am currently taking a PHP class, but unfortunately, the instructor seems to lack expertise in this area. Initially, I declared the array as global and stored som ...

Utilizing JavaScript Files Instead of NPM as a Library for Open Layers: A Step-by-Step Guide

I've been attempting to get Open Layers to function in my Eclipse web development environment, but I've encountered some challenges along the way. The setup instructions provided on the Open Layers website focus mainly on using npm. Nevertheless, ...

Save the promise object from the $http GET request in a local storage

Struggling to wrap my head around how to update data on a graph without the proper Angular service knowledge. All I want is to retrieve a JSON object with one GET request and save it locally on my controller. This way, I can use the original JSON to displa ...

Tips for accessing the current state/value in a third-party event handler?

Consider the following scenario: function MapControl() { const [countries, setCountries] = useContext(CountriesContext) useEffect( () => { ThirdPartyApi.OnSelectCountry((country) => { setCountries([...countries, country]) }) }) ...

Which specific web framework supports the functionalities of Microsoft Dynamics CRM 2011?

Is there an SDK available from Microsoft that can help me create a web product with similar rich ajax features as Microsoft Dynamics CRM 2011? I have considered using Microsoft SharePoint Foundation 2010, but I am concerned that it is designed for small o ...

Tips and tricks for avoiding character escaping in JSTL with c:out

For my project, I am utilizing JSTL <c:out>. Currently, I have a string coming from the servlet that looks like this: "2\'000;11\'222;10\'333". In JavaScript, I want to split it into separate values like 2'000;11&ap ...

Sending a file stream with specific file name and extension in NodeJS: A comprehensive guide

Currently, I am utilizing nodejs to transmit file streams to express response var fileReadStream = fs.createReadStream(filePath); fileReadStream.pipe(res); However, the issue arises on the front-end where the file is solely downloadable with the "download ...

In Vue, the concept of using the debounce method is not clearly defined

I am aware that using the arrow syntax for a method can cause 'this' to not be mapped to the Vue instance. In my case, I am utilizing lodash.debounce and I don't think I'm using the arrow syntax here? Encountering Error: Cannot read pr ...

Javascript eval function providing inaccurate results

I have encountered a problem while using eval() for a calculator I am developing. When I input the following code: console.log(eval("5.2-5")); The output is: 0.20000000000000018 I am confused as to why this is happening. Thank you for your assistance. ...

Is there a way to implement jQuery on dynamically added content?

I created a page that closely resembles a Facebook wall, with posts, comments for each post, and a "send" button to add new comments. In this example: The posts are identified by the class "mypost". The "send-comment" button is located within a div with ...

Divide JSON arrays into separate select boxes

I have integrated AngularJS into certain sections of my website, rather than using it for the entire site. Currently, I am dealing with a scenario where I have a pair of select boxes, one dependent on the other. One box contains a list of countries, while ...

The AngularJS Input Validation TypeError occurs when attempting to use an undefined function for validation

Currently, I am working on input validation using Angular. However, when the function is triggered, all elements return as undefined. The crucial code snippets are provided below, but do let me know if you need more information. Your assistance is greatly ...

Modifying the Lettering of a String

Recently delving into C programming, I attempted to create a basic function that would change the case of a string (or character array) following a specific set of rules: The first letter should be capitalized. The rest of the characters should be in low ...