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
How can I simplify a nested object which has a lot of repetitions?

All these three objects have the exact same property and values. Is there any way to simplify the code and make it shorter?

const MyMap = {
  'myObject': {
    propone: 'One',
    proptwo: 'Two',
    propthree: 'Three'
  },
  'yourObject': {
    propone: 'One',
    proptwo: 'Two',
    propthree: 'Three'
  },
  'hisObject': {
    propone: 'One',
    proptwo: 'Two',
    propthree: 'Three'
  }
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can store the object in its own variable and spread it into MyMap:

const innerObject = {
  propone: 'One',
  proptwo: 'Two',
  propthree: 'Three',
};
const MyMap = {
  myObject: { ...innerObject },
  yourObject: { ...innerObject },
  hisObject: { ...innerObject }
};

console.log(MyMap);
.as-console-wrapper {
  max-height: 100% !important;
  top: auto;
}

Or for something which allows greater flexibility in the variable names:

const innerObject = {
  propone: 'One',
  proptwo: 'Two',
  propthree: 'Three',
};
const propNames = ['myObject', 'yourObject', 'hisObject'];
let MyMap = propNames.reduce((a, c) => {
  a[c] = { ...innerObject };
  return a;
}, {});

console.log(MyMap);
.as-console-wrapper {
  max-height: 100% !important;
  top: auto;
}

With even more simplicity as suggested by Matt Morgan:

const MyMap = propNames.reduce((a, c) => ({...a, [c]: {...innerObject}}), {});

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!