无法从对象中获取数据[重复]

时间:2021-10-07 09:11:32

This question already has an answer here:

这个问题在这里已有答案:

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.

我有java脚本对象,我想从对象中获取值,但我无法从对象中获取值。

Here is my javascript object

这是我的javascript对象

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?

2 个解决方案

#1


1  

Like this:

var Username = Profile["[Username]"];
var First_name = Profile["[Name | First]"];
var Last_name = Profile["[Name | Last]"];

#2


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]']);

#1


1  

Like this:

var Username = Profile["[Username]"];
var First_name = Profile["[Name | First]"];
var Last_name = Profile["[Name | Last]"];

#2


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]']);