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

181
Views
StartsWith() in an array

Basically I am using the values 'FLD_STR_101' to retrieve my file. I have different field starting with 'FLD_STR_' so I cannot base my if statement on this specific field. What I would like is to to do is map and retrieve the field with FLD_STR...something like this

values[startWith('FLD_STR_')]

so then I will be able to check if the field starts with FLD_STR_ and then I will be able to differentiate the field depending on the type of each field(file, text ...)

Here is what I have as an example so you can understand. It seems that I cannot inject the startsWith() inside the array like this. Any clue on how to achieve this ?

    const test =Object.entries(values['FLD_STR_101']).map((entry, key) =>( {
            test: entry[0],
            test2:key
    }))

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

An idea can be

  1. to filter the object key that match your expectation
  2. loop with array.forEach method on these filtered keys
  3. doing your map method on values[filteredKey]

const values = {
  FLD_STR_101: {
    test: 1,
    type: 'type1'
  },
  FLD_STR_102: {
    test2: 1,
    type: 'type1'
  },
  FLD_STR_103_NO_TYPE: {
    test2: 1
  },
  NOTFLD_STR_102: {
    test3: 1
  }
};

let test = [];

Object.keys(values)
  .filter(key =>  key.startsWith('FLD_STR_') && values[key]['type'])
  .forEach(filteredKey => {
    test = [
       ...test, 
       ...Object.entries(values[filteredKey]).map((entry, key) => ({
         test: entry[0],
         test2: key
       }))]
    });

console.log(test);

about 4 years ago · Santiago Trujillo Report

0

You were almost there, you can use an "startWidth" method, but not directly, try combining with the array method filter, it's cleaner and readable.

const values = {
  FLD_STR_101: {
    test: 1
  },
  FLD_STR_102: {
    test2: 2
  },
  INVALID_STR_103: {
    test3: 3
  }
};
const startWith = (str, prefix) =>  {
  return str.slice(0, prefix.length) === prefix;
}

const test = Object.entries(values)
  .filter(([key]) => startWith(key, 'FLD_STR_'))
  .map((entry, key) =>( {
    test: entry[0],
    test2:key
  }))
about 4 years ago · Santiago Trujillo 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!