通过将对象数据存储在变量中来打印数据

时间:2018-08-01 18:39:29

标签: javascript object

我有一个名为data的对象,并且在数据控制台上,我做到了:

var data={
     countrycode: "376",
     dob: "2017-05-24",
     email: "bsbsbs@gmail.com",
     firstName: "ggsgsggsg",
     gender: "male",
     phoneNumbers: "88888888888888888",
     surName: "hshshhshs"
    }

let countrycode,iso2,number,firstName,surName,dob,gender,email;
countrycode=data.countrycode;
number=data.phoneNumbers;
firstName=data.firstName;
surName=data.surName;
email:data.email;
gender:data.gender;
dob:data.dob;
console.log('email is',email);
console.log('number is ',number);
console.log('gender is',gender);
console.log('surName is',surName);
console.log('dob is',dob);
console.log('firstName is',firstName);

我看到的是,当我在控制台上记录这些值时,我对电子邮件,性别和dob的定义未定义,而对于其他其余部分,我得到了正确的值。 这可能是什么原因,为什么会发生?

2 个答案:

答案 0 :(得分:1)

在给出的示例中,您必须实际使用=作为赋值运算符,冒号用于对象属性赋值。

email = data.email;
gender = data.gender;
dob = data.dob;

答案 1 :(得分:0)

使用=进行分配,而不使用:(保留给对象的属性分配值)。

应该是

email=data.email;

不是

email:data.email;

var data={
     countrycode: "376",
     dob: "2017-05-24",
     email: "bsbsbs@gmail.com",
     firstName: "ggsgsggsg",
     gender: "male",
     phoneNumbers: "88888888888888888",
     surName: "hshshhshs"
    }

let countrycode,iso2,number,firstName,surName,dob,gender,email;
countrycode=data.countrycode;
number=data.phoneNumbers;
firstName=data.firstName;
surName=data.surName;
email=data.email;
gender=data.gender;
dob=data.dob;
console.log('email is ',email);
console.log('number is ',number);
console.log('gender is ',gender);
console.log('surName is ',surName);
console.log('dob is ',dob);
console.log('firstName is ',firstName);