0

I have this object in javascript.

info = { bio: {Age:21,Name:"John",Gender:"Male"}, qualification:{matricscore: 92, highschool: 80} }

I want to create a new object profile in which I want the value of the key bio. It will look somewhat like this.

profile = { Age:21,Name:"John",Gender:"Male" } 

What should I do?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • `Object.assign(profile, info.bio)` will copy the inner object so that changes to info.bio won't be reflected in profile – danh Mar 11 '21 at 04:51

4 Answers4

1

info = { bio: {Age:21,Name:"John",Gender:"Male"}, qualification:{matricscore: 92, highschool: 80} }


const profile = JSON.parse(JSON.stringify(info['bio']));
console.log(profile);
Not A Bot
  • 2,474
  • 2
  • 16
  • 33
0

I think you're looking for:

const profile = info.bio;
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • This works but the problem is it puts values in the profile using pass by reference method, so when I tried to change the profile, the changes got reflected in info.profile also. – Shubhang Chourasia Mar 11 '21 at 10:53
  • That is expected behavior. JavaScript is a pass-by-value language. See https://stackoverflow.com/questions/13104494/does-javascript-pass-by-reference – Frank van Puffelen Mar 11 '21 at 15:49
0

Maybe you are also missing quotes, It will look something like:

var profile = info.bio;

Also, change your dictionary to:

{"bio":{"Age":21,"Name":"John","Gender":"Male"},"qualification":{"matricscore":92,"highschool":80}}
Lakshaya U.
  • 1,089
  • 1
  • 5
  • 21
  • This works but the problem is it puts values in the profile using pass by reference method, so when I tried to change the profile, the changes got reflected in info.profile also. – Shubhang Chourasia Mar 11 '21 at 10:53
0

var profile = info.bio;

function myFunction() {
  
  info = { bio: {Age:21,Name:"John",Gender:"Male"}, qualification:{matricscore: 92, highschool: 80} };
 console.log( info.bio ); 
}
<!DOCTYPE html>
<html>
<body>

<h1>Click to </h1>

<button onclick="myFunction()">Click and see console</button>

</body>
</html>

Simple way to get bio from info.