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

249
Views
JavaScript array or object checking - code improvements

I receive data => these data could be array of object or just a object. I write some code, but maybe there is a way to make this code more sexy, clear, or shorter plus without any errors

Here is the code:

export const CalculateIt = (props) => {
  const conversionValues = []
  if (props) {
    if (props.length > 0) {
      for (let i = 0; i < props.length; i++) {
        const clicks = props[i]?.insights?.[0].inline_link_clicks
        const actionsNumber = props[i]?.insights?.[0]?.actions?.length || 0
        let result = 0
        if (clicks && actionsNumber) {
          result = devideNumbers(clicks, actionsNumber, 8)
        }
        conversionValues.push(result)
      }
      return conversionValues
    }
    const clicks = props?.insights?.[0].inline_link_clicks
    const actionsNumber = props?.insights?.[0]?.actions?.length || 0
    let result = 0
    if (clicks && actionsNumber) {
      result = devideNumbers(clicks, actionsNumber)
    }

    return conversionValues.push(result)
  }
}

As you can see there you can find some parts of the code that are similar like:

const clicks = props[i]?.insights?.[0].inline_link_clicks

and 

const clicks = props?.insights?.[0].inline_link_clicks

Is it possible to write it more smart?

Best

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

0

In fact, you can force all data into a one-dimensional array and safe work with array of objects only.

Is this code sexy enough?

const obj = {id: 1, val: 1};
const arr = [{id: 1, val: 1},{id: 2, val: 2},{id: 3, val: 3}];

const normalize = (data) => [data].flat();
console.log(normalize(obj)[0]);
console.log(normalize(arr)[0]);

// -------------------
// and you can use this approach in code:
const getResult = (obj) => obj.val * 10;
const CalculateIt = (props) => [props].flat().map(getResult);

console.log(CalculateIt(obj));
console.log(CalculateIt(arr));
.as-console-wrapper{min-height: 100%!important; top: 0}

about 4 years ago · Juan Pablo Isaza Report

0

Probably move the common code in a function:

function getResult(data) {
    const clicks = data?.insights?.[0].inline_link_clicks
    const actionsNumber = data?.insights?.[0]?.actions?.length || 0
    let result = 0
    if (clicks && actionsNumber) {
       result = devideNumbers(clicks, actionsNumber, 8)
    }
    return result;
}

And use the helper function in your original function:

export const CalculateIt = (props) => {
  const conversionValues = []
  if (props) {
    if (props.constructor === Array) {
      props.forEach((item) => conversionValues.push(getResult(item)))
    } else {
       conversionValues.push(getResult(props));
    }
    return conversionValues;
  }
}

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!