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

127
Views
Filter an array of objects by keys in Javascript

I have gone through several similar posts about filtering the array based on the keys and tried implementing it as well but not able to get the correct answer. Here's my code -

const data = [{
  dnum: "061806",
  enddate: "2022-05-31",
  fnum: 0.7,
  role: "MP",
  startdate: "2022-01-13",
}, {
  dnum: "061806",
  enddate: "2022-05-31",
  fnum: 0.786,
  role: "MP",
  startdate: "2022-01-13",
}]

function copyObjectProps(source, keys) {
  let newObject = {}
  keys.forEach(function(key) {
    newObject[key] = source[key]
  })
  return newObject
}

let filteredObject = copyObjectProps(data, ['dnum', 'role', 'fnum'])

console.log(filteredObject)

I am getting the below result -

{
  dnum: undefined,
  role: undefined,
  fnum: undefined
}

But I am expecting it to be something like this -

[{
  dnum: "061806",
  fnum: 0.7,
  role: "MP",
}, {
  dnum: "061806",
  fnum: 0.786,
  role: "MP",
}]

Could anyone tell me what exactly I am doing wrong?

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

0

see this article

const data = [{
dnum: "061806",
enddate: "2022-05-31",
fnum: 0.7,
role: "MP",
startdate: "2022-01-13",
},{
dnum: "061806",
enddate: "2022-05-31",
fnum: 0.786,
role: "MP",
startdate: "2022-01-13",
}]

function selectProps(props){
  return function(obj){
    const newObj = {};
    props.forEach(name =>{
      newObj[name] = obj[name];
    });
    
    return newObj;
  }
}
let filteredObject1 = data.map(selectProps(['dnum', 'role','fnum']))
let filteredObject2 = data.map(selectProps(['dnum','fnum']))

console.log(filteredObject1)
console.log(filteredObject2)

edit if want to change key names i did something like this

const data = [{
dnum: "061806",
enddate: "2022-05-31",
fnum: 0.7,
role: "MP",
startdate: "2022-01-13",
},{
dnum: "061806",
enddate: "2022-05-31",
fnum: 0.786,
role: "MP",
startdate: "2022-01-13",
}]

function selectProps(props){
  return function(obj){
    const newObj = {};
    props.forEach(el =>{
      newObj[el[1]] = obj[el[0]];
    });
    
    return newObj;
  }
}
let filteredObject = data.map(selectProps([['dnum','DNUM'],['fnum','fnum'],['role','Role Name']]))
//if dont want to change key name send like this ['dnum','dnum']
console.log(filteredObject)

about 4 years ago · Juan Pablo Isaza Report

0

You have almost nailed it. Here is what you need to change to make it work:

// modify this line
let filteredObject = copyObjectProps(data, ['dnum', 'role', 'fnum'])

// to this
const filteredObject = data.map(item => {
  return copyObjectProps(item, ['dnum', 'role', 'fnum']))
}

Here is what happend:

  1. You are building the new Object right.
  2. You have missed that you need an Array of Objects. map is giving you the Array, your method is giving you the right Object. Good luck! 🙂
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!