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

135
Views
populate object with new object in javascript

how do I combine two objects so that it looks like the following code

{
  category: 'product 1',
  sub_category: 'sub product 1',
}
{
  nama: "nama product1",
  jenis: "jenis1"
}

{
  nama: "nama product1",
  jenis: "jenis1",
  category: {
    category: 'product 1',
    sub_category: 'sub product 1',
  }
}

Please help me

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

0

Use spread

const x = {
  category: 'product 1',
  sub_category: 'sub product 1',
}
const y = {
  nama: "nama product1",
  jenis: "jenis1"
};

const z = {...y, category: x};

const log = document.querySelector(`pre`);

log.textContent = `nested:\n` + JSON.stringify(z, null, 2);

// or flattened:
const zz = { ...y, ...{ category: x.category, sub_category: x.sub_category } };
log.textContent += `\n\nflat:\n${JSON.stringify(zz, null, 2)}`;
<pre></pre>

about 4 years ago · Juan Pablo Isaza Report

0

You could do something simple like this, assuming that all objects have the same format

const obj1 = {
    category: 'product 1',
    sub_category: 'sub product 1',
}
const obj2 = {
    nama: "nama product1",
    jenis: "jenis1"
}

obj2.category = obj1;
console.log(obj2);

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!