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
Get function pointers for array appending

I want to determine whether an item should be looped and pushed, or directly pushed to an array.

For this, I have created the following code:

interface Item {
  prop1: string;
  prop2: string;
  prop3: string[];
}

For prop1, prop2, prop3 I need its data in a single string array for each property. So I want to get the corresponding function pointer:

const items = [...] as Item[];
const collectProperties = ["prop1", "prop2", "prop3"];

// Contains the function pointers for every prop in collectProperties
const appendOperations = [] as ((array: string[], element: any) => void)[];

// Extract function pointers for each prop-type
collectProperties.forEach((element, index) => {
  appendOperations.push(
    Array.isArray(items[0][element as keyof Item])
      ? (array: string[], element: any) =>
          element.forEach((value) => array.push(String(value)));
      : (array: string[], element: any) =>
          array.push(String(element))
  );
});

// Appending items based on its type
items.forEach((item) => {
  appendOperations.forEach((fn, index) => {
    fn(
      // ... some array,
      // ... some value
    );
  });
});

I feel like there should be a more compact, elegant way of doing this. I know this can be done in the loop itself as well, but this means there will be an if-check in every property of every element, which is not desired when processing huge chunks of data. Help is appreciated.

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

0

There are a couple of things you can do to tighten up the first bit:

  • Make collectProperties an array of keyof Item rather than an array of strings, so you don't have to use a type assertion later
  • Use map to build appendProperties rather than creating the array and pushing to it, since it's a one-to-one operation; this also lets you infer its type from the map return type
// *** Use the type on the array so you don't have to use a type assertion later
const collectProperties: (keyof Item)[] = ["prop1", "prop2", "prop3"];

// Contains the function pointers for every prop in collectProperties
// *** `map` is a bit more idiomatic and helps a bit with setting types
const appendOperations = collectProperties.map(propName => {
    // *** Avoiding using `items` for this check since there's no need to make this function
    // dependent on `items`
    return propName === "prop3"
        ? (array: string[], element: any) =>
            element.forEach((value: any) => array.push(String(value)))
        : (array: string[], element: any) =>
            array.push(String(element));
});

// Appending items based on its type
// *** `forEach` is fine, but you could also avoid making functions using `for-of`
for (const item of items) {
    for (const fn of appendOperations) {
        fn(
            // ... some array,
            // ... some value
        );
    }
}

For the loop at the end, I couldn't get enough of an idea what it should be doing, but you might consider for-of rather than forEach just to avoid creating unnecessary functions, etc.

Playground link

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!