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

109
Views
How to map through array of objects and create a new array of objects with a new added property using react and javascript?

Hi i have array of objects like below,

const arr_obj = [ 
    {
        children: [{}],
        type: "type1",
        updated: "somevalue",
    },
    {
         children: [{}],
         type: "type2",
         updated: "somevalue",
     },
]

now i want to create a new array of object that will have each object as in arr_obj and also add new property "disabled" which is boolean value set to true if type is equal to "type2" if not set to false.

so the expected output is like below

const new_obj = [
    {
        children: [{}],
        type: "type1",
        updated: "somevalue",
        disabled: false,
    },
    {
        children: [{}],
        type: "type2",
        updated: "somevalue",
        disabled: true,
    },
]
    

I have tried like below

const new_obj = React.useMemo(() => {
    arr_obj.map((arr => ({
        ...arr,
        disabled: arr?.type !== "type2" ? false : true,
    }))
}, [arr_obj]
);

but when i log new_obj this is undefined. could someone help me with this. I am new to programming. thanks.

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

0

You're not returning the result of arr_obj.map so the function returns undefined.

Either

const new_obj = React.useMemo(() => {
  return arr_obj.map((arr) => ({
    ...arr,
    disabled: arr?.type !== "type2" ? false : true,
  }));
}, [arr_obj]);

or drop the braces:

const new_obj = React.useMemo(
  () =>
    arr_obj.map((arr) => ({
      ...arr,
      disabled: arr?.type !== "type2" ? false : true,
    })),
  [arr_obj],
);
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!