Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

391
Views
JavaScript, filter array of objects by a specific key's value

I am trying to find the best way to filter my array of objects with specific key's string. Basically what I am trying to achieve is to get the objects which contain "Type":"Blue". Here is my data:

[
   {
       "data": [
           {}
       ],
       "Name": "1",
       "Type": "Blue"
   },
   {
       "data": [
           {}
       ],
       "Name": "2",
       "Type": "Red"
   },
   {
       "data": [
           {}
       ],
       "Name": "3",
       "Type": "Blue"
    }
] 
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could use the filter method. See the snippet below, as well as a definition of the method from MDN:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

const data = [
  {
    data: [{}],
    Name: "1",
    Type: "Blue"
  },
  {
    data: [{}],
    Name: "2",
    Type: "Red"
  },
  {
    data: [{}],
    Name: "3",
    Type: "Blue"
  }
];

const filteredData = data.filter((item) => item.Type === "Blue");
console.log(filteredData);

about 4 years ago · Juan Pablo Isaza Report

0

You can use the Array.prototype.filter() method.

arr.filter(obj => obj.Type == 'Blue');

will give you the array containing only the objects with type Blue.

about 4 years ago · Juan Pablo Isaza Report

0

If I understood your requirement correctly, You have a string "Type: Blue" and based on this string you have to filtered out the data array dynamically based on the key as Type and value as Blue. If Yes, Here you go :

const str = "Type: Blue";

const splittedStr = str.split(':');

const data = [
  {
    Name: "1",
    Type: "Blue"
  },
  {
    Name: "2",
    Type: "Red"
  },
  {
    Name: "3",
    Type: "Blue"
  }
];

const filteredData = data.filter((item) => item[splittedStr[0]] === splittedStr[1].trim());

console.log(filteredData);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!