React by Patrik

Extracting Specific Fields After Filtering in JavaScript

Overview: After filtering data, you may only need specific fields, such as the name attribute. This Snipp explains how to use the map() method to extract specific fields from filtered data.

Implementation:

// Filter and extract only the names
const germanNames = namesWithAttributes
  .filter(person => person.language === "German")
  .map(person => person.name);
console.log(germanNames);
// Output: ["Norbert"]

Here, after filtering by language, we use map() to return just the name values from the filtered array. This results in an array of names that meet the filtering criteria.

JavaScript
Filter
Fields

Comments