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

203
Views
Filter objects in an array based on regex applied to property

I have this data structure:

const data = [
  {
    id: "1",
    name: "first item",
    T1_something: 21,
    T1_anotherthing: 15,
    T1_hello: 98,
    T2_coding: 12.3,
    T2_world: 4.1,
    T2_different: 2.5
  },
  {
    id: "2",
    name: "second item",
    T1_something: 3,
    T1_anotherthing: 12.6,
    T1_hello: 64.1,
    T2_coding: 71.3,
    T2_world: 4.5,
    T2_different: 3.3
  }
];

I want to get only properties where the property name matches the following regex /^T\d/

So my desired output is this:

const desiredResult = [
  {
    T1_something: 21,
    T1_anotherthing: 15,
    T1_hello: 98,
    T2_coding: 12.3,
    T2_world: 4.1,
    T2_different: 2.5
  },
  {
    T1_something: 3,
    T1_anotherthing: 12.6,
    T1_hello: 64.1,
    T2_coding: 71.3,
    T2_world: 4.5,
    T2_different: 3.3
  }
];

How do I go about doing this filtering based on a regex in an array of objects?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

My solution

const desiredResult = data.map(o=>{
    const newO={};
    Object.keys(o).filter(oKey=>/^T\d/.test(oKey)).forEach(oKey=>{
        newO[oKey]=o[oKey]
    })

    return newO;
})
about 4 years ago · Juan Pablo Isaza Report

0

const data = [
  {
    id: "1",
    name: "first item",
    T1_something: 21,
    T1_anotherthing: 15,
    T1_hello: 98,
    T2_coding: 12.3,
    T2_world: 4.1,
    T2_different: 2.5
  },
  {
    id: "2",
    name: "second item",
    T1_something: 3,
    T1_anotherthing: 12.6,
    T1_hello: 64.1,
    T2_coding: 71.3,
    T2_world: 4.5,
    T2_different: 3.3
  }
];

const filterBasedOnProp = (elements, pattern) => {
  const regex = new RegExp(pattern)
  
  return elements.map((element) => Object.keys(element).reduce((accumulator, key) => {
    if (key.match(regex)) accumulator[key] = element[key]
    
    return accumulator
  }, {}))
}

console.log(filterBasedOnProp(data, "^T"))

about 4 years ago · Juan Pablo Isaza Report

0

// const data = ...
// const regexp = ...

function filterMyDataProperties(data, regexp) {
  return data.map(item => {
    return Object.keys(item).reduce((newItem, key) => {
      if (regexp.test(key)) {
        newItem[key] = item[key]
      }
  
      return newItem
    }, {})
  })
}

const result = filterMyDataProperties(data, regexp);
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!