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

118
Views
Typescript filter to select object with value of specific words

I have an array of objects in my DB records.

The record:

{"ID123":{"FileID":"12345","FileName":"ABCFile_ver5_44453.PDF"},"DocID":6009,"DocFormat":"PDF"}

The format to store the filename in my DB is always with "FileName": "actual_fileName.PDF".

I want to only get the object with "FileName":"....", to only display the filename instead of other objects.

This is my code:

getValue(): Atts[] | any {
    if (this.isEmptyValue()) {
      return [];
    }
    return !this.useObject ? this.value : Object.entries(this.value).map(([key, value]) => ({ key, value })).filter(value=);
  }

How do I filter the object that contains "FileName" so that I can display the filename in my application? I'm stuck at the filter method.

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

0

I had to reduce your code a little to a minimal reproducible example, and I made the assumption that this.value in your Object.entries is the entire DB record that you have listed above. I converted this to an object so I could process it in my code. So, your mileage may vary if my assumption was incorrect.

let obj = {
  "ID123": {
    "FileID": "12345",
    "FileName": "ABCFile_ver5_44453.PDF"
  },
  "DocID": 6009,
  "DocFormat": "PDF"
};

let result = { "FileName": Object.entries(obj).map(([key, value]) => ({
  key,
  value
})).filter(keyValuePair => {
  if (keyValuePair.value.FileName) {
    return true;
  }
})[0].value.FileName };

This returns:

{
  "FileName": "ABCFile_ver5_44453.PDF"
}

Your filter step is filtering an array of key value pairs, so when filtering, you need to return true only if the 'value' is an object that has a property of FileName.

EDIT:

I realized that the way I wrote this will not work if the returned object from the array does not have value (or is undefined), so it's probably a better idea to store this in a variable first, and then return an object based on that varible, like so:


let fileNameObj = Object.entries(obj).map(([key, value]) => ({
  key,
  value
})).filter(keyValuePair => {
  if (keyValuePair && keyValuePair.value.FileName) {
    return true;
  }
})[0];

if (fileNameObj && fileNameObj.FileName) {
  let result = { "FileName": fileNameObj.FileName };
}

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!