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

173
Views
How to merge an object inside another object

I don't know if the question is right, but I want to do the following, I want the properties of "variable" to become part of the variable day.

The object being as follows: {seg: 60, min:1, dias: 7, semana: 1}

Remaining the object of the following way: Take into consideration that the object inside the object could be anyone and it does not have to be the property "variable" for what I would like to make it dynamic, reason why an arrangement like delete dia.variable and later to merge would not serve me.

With the code that I show you I have only obtained that the result is: {seg: 60, min:1, variable:{dias: 7, semana: 1}, dias: 7, semana: 1}

Any ideas on how I could do it more efficiently, and above all, do it.

const dia = {seg: 60, min:1, variable:{dias: 7, semana: 1}}
let varia = {}
let newDia = {}

const processJson = () => {
    Object.entries(dia).map(([name, value]) => {
        if(typeof(value) === 'object'){
            varia = value
            newDia = {...dia, ...variable}
        }
    })    
}

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

0

This is a universal method for flatting any object with 2 levels deep. It uses the array reduce method and the Object.keys() function:

const dia = { seg: 60, min: 1, variable: {dias: 7, semana: 1} }

function flatObject(o) {
  return Object.keys(o).reduce((result, key) => {
  
    // If there is a nested object, flat the object
    if (typeof o[key] === 'object' && ! Array.isArray(o[key]) && o[key] !== null) {
      for (const inKey in o[key]) {
        result[inKey] = o[key][inKey];
      }
    }
    // Just copy the value
    else {
      result[key] = o[key];
    }
    
    // Return accumulator
    return result;
  }, {});
}

console.log(flatObject(dia))

about 4 years ago · Juan Pablo Isaza Report

0

Start of by using a destructuring assignement to extract the seg, min and variable properties from the dia object.

const { seg, min, variable } = dia;

Then create a new object in which you set the seg and min properties with the destructured properties. For the variable property, use the spread syntax to place the properties of variable inside the same object.

const result = {
  seg,
  min,
  ...variable
};

const dia = {
  seg: 60,
  min: 1,
  variable: {
    dias: 7,
    semana: 1
  }
};

const { seg, min, variable } = dia;

const result = {
  seg,
  min,
  ...variable
};

console.log(result);

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!