1

I am trying that a variable that I have just defined has the value of an object and that I can also add more information to it at the same "level"

Something like that:

let obj1 = {
  name: "jhon",
    address: {
      city: "abc123",
      country: "def456",
      zipcode: "123456",
    },
    job: {
      position: "qwe123",
      experience: "12 years",
    },
}

let obj2 = {
  info1: "abc123456",
  info2: "12345678978",
  info3: "1234567d78778",
}

let myobj = {

  name: "jhon",
  address: {
    city: "abc123",
    country: "def456",
    zipcode: "123456",
  },
  job: {
    position: "qwe123",
    experience: "12 years",
  },
  
  info1: "abc123456",
  info2: "12345678978",
  info3: "1234567d78778",
  
}

console.log(obj1)
console.log(obj2)
console.log(myobj)

Let me explain, I want "myobj" to be equal to obj1 and obj2.

obj1 and obj2 come from different parts and I want the data to be in "myobj" at the same level, as shown in the code

Ale
  • 117
  • 1
  • 2
  • 14

1 Answers1

0

though its duplicate question you can use jquery like below

let obj1 = {
  name: "jhon",
    address: {
      city: "abc123",
      country: "def456",
      zipcode: "123456",
    },
    job: {
      position: "qwe123",
      experience: "12 years",
    },
}

let obj2 = {
  info1: "abc123456",
  info2: "12345678978",
  info3: "1234567d78778",
}

let myobj = $.extend( obj1, obj2 );
console.log(obj1)
console.log(obj2)
console.log(myobj)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

or js like that

let obj1 = {
  name: "jhon",
    address: {
      city: "abc123",
      country: "def456",
      zipcode: "123456",
    },
    job: {
      position: "qwe123",
      experience: "12 years",
    },
}

let obj2 = {
  info1: "abc123456",
  info2: "12345678978",
  info3: "1234567d78778",
}

let myobj = Object.assign({},obj1,obj2)
console.log(obj1)
console.log(obj2)
console.log(myobj)

// or use es6 syntext

console.log('//////////////////////////////////////')

let myobjes6 = {...obj1,...obj2}
console.log(obj1)
console.log(obj2)
console.log(myobjes6)
ANIK ISLAM SHOJIB
  • 3,002
  • 1
  • 27
  • 36