1

I have java script object and i want to fetch the value from object but i am unable to fetch the values from the object.

Here is my javascript object

var profile = { '[Profile ID]': 135675302,
  '[Name | Prefix]': '',
  '[Name | First]': 'KK',
  '[Name | Middle]': '',
  '[Name | Last]': 'Test',
  '[Contact Name]': 'KK Test',
  '[Email | Primary]': 'kk@yopmail.com',
  '[Email | Main]': 'kk@yopmail.com',
  '[Address | Primary | Line 1]': '',
  '[Address | Primary | Line 2]': '',
  '[Address | Primary | City]': '',
  '[Address | Primary | State]': '',
  '[Address | Primary | Zip]': '',
  '[Address | Primary | Country]': '',
  '[Address | Main | Line 1]': '',
  '[Address | Main | Line 2]': '',
  '[Address | Main | City]': '',
  '[Address | Main | State]': '',
  '[Address | Main | Zip]': '',
  '[Address | Main | Country]': '',
  '[Phone | Primary]': '',
  '[Organization]': '',
  '*Key Contact': '',
  Facebook: '',
  'Gender Served': [],
  LinkedIn: '',
  'School Size': [],
  'School Website': '',
  'Student Residential Status': [],
  Title: '',
  Twitter: '',
  '[Created Date]': '05/11/2018 11:49:56 AM',
  '[Expiration Date]': '',
  '[Group]': [ 'Members' ],
  '[Last Modified Date]': '05/11/2018 11:49:56 AM',
  '[Member Status]': 'Active',
  '[Member Type]': 'Members',
  '[Username]': 'kk@yopmail.com' }

I want to fetch "First name", "Last name" and "Username" from the object.

Any Idea?

arun kamboj
  • 1,145
  • 4
  • 17
  • 48

2 Answers2

2

Use bracket notation when the property value can't be accessed with dot notation - just put the property value in a string inside the brackets:

var profile = { '[Profile ID]': 135675302,
  '[Name | Prefix]': '',
  '[Name | First]': 'KK',
  '[Name | Middle]': '',
  '[Name | Last]': 'Test',
  '[Contact Name]': 'KK Test',
  '[Email | Primary]': 'kk@yopmail.com',
  '[Email | Main]': 'kk@yopmail.com',
  '[Address | Primary | Line 1]': '',
  '[Address | Primary | Line 2]': '',
  '[Address | Primary | City]': '',
  '[Address | Primary | State]': '',
  '[Address | Primary | Zip]': '',
  '[Address | Primary | Country]': '',
  '[Address | Main | Line 1]': '',
  '[Address | Main | Line 2]': '',
  '[Address | Main | City]': '',
  '[Address | Main | State]': '',
  '[Address | Main | Zip]': '',
  '[Address | Main | Country]': '',
  '[Phone | Primary]': '',
  '[Organization]': '',
  '*Key Contact': '',
  Facebook: '',
  'Gender Served': [],
  LinkedIn: '',
  'School Size': [],
  'School Website': '',
  'Student Residential Status': [],
  Title: '',
  Twitter: '',
  '[Created Date]': '05/11/2018 11:49:56 AM',
  '[Expiration Date]': '',
  '[Group]': [ 'Members' ],
  '[Last Modified Date]': '05/11/2018 11:49:56 AM',
  '[Member Status]': 'Active',
  '[Member Type]': 'Members',
  '[Username]': 'kk@yopmail.com' };
  
  console.log(profile['[Name | First]']);
  console.log(profile['[Name | Last]']);
  console.log(profile['[Username]']);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

Like this:

var Username = Profile["[Username]"];
var First_name = Profile["[Name | First]"];
var Last_name = Profile["[Name | Last]"];
Darius Buhai
  • 334
  • 1
  • 12