如何访问嵌套对象中的键

时间:2019-12-17 06:30:37

标签: javascript object nested

{
  "5def224170a3ec0d90dbb89a": {
    "_id": "5def224170a3ec0d90dbb89a",
    "username": "dhruv1999",
    "fullname": "dhruv parmar",
    "email": "dhruv@gmail.com",
    "phone": "7045691556",
    "password": "$2a$10$UOk8E4fbkkZvSRSzQqWK4eyJ2Z.qtL4D38thfhzkxJ1pSxSFn5fna",
    "__v": 0
  },
  "5def2747d135311a747a9660": {
    "_id": "5def2747d135311a747a9660",
    "username": "dhruv1998",
    "fullname": "parmar",
    "email": "dhruvkp@gmail.com",
    "phone": "704591556",
    "password": "$2a$10$px5SK.OpP7i2Ryvg.ko/OeIbuq9XDT3luTrIBloNFCTiImUJC1BIm",
    "__v": 0
  }
}

我如何获取所有电子邮件,例如dhruv @ gmail.com,dhruvkp @ gmail.com

我尝试做但没有收到电子邮件。

const userMap = {};
users.forEach(user => {
  userMap[user._id] = user;
});

userMap.forEach((obj) => {
      keys = Object.keys(obj);
      keys.forEach((key) => {
        console.log(obj[key].email);

      });

8 个答案:

答案 0 :(得分:2)

以下操作不起作用的原因是因为userMap是一个对象(您也没有关闭外部.forEach循环,这将是语法错误):

userMap.forEach((obj) => {
  keys = Object.keys(obj);
  keys.forEach((key)=>{
    console.log(obj[key].email);
  });

要使用.forEach(),您需要在数组(或类似数组的集合)上使用它。相反,您可以获取对象的Object.values()Object.values()返回一个数组,因此您可以在其上使用.map()来获取电子邮件属性:

const obj = {"5def224170a3ec0d90dbb89a": { "_id": "5def224170a3ec0d90dbb89a", "username": "dhruv1999", "fullname": "dhruv parmar", "email": "dhruv@gmail.com", "phone": "7045691556", "password": "$2a$10$UOk8E4fbkkZvSRSzQqWK4eyJ2Z.qtL4D38thfhzkxJ1pSxSFn5fna", "__v": 0 }, "5def2747d135311a747a9660": { "_id": "5def2747d135311a747a9660", "username": "dhruv1998", "fullname": "parmar", "email": "dhruvkp@gmail.com", "phone": "704591556", "password": "$2a$10$px5SK.OpP7i2Ryvg.ko/OeIbuq9XDT3luTrIBloNFCTiImUJC1BIm", "__v": 0 } }

const res = Object.values(obj).map(({email}) => email);
console.log(res);
console.log(res.join(', ')); // use `.join()` for string output

答案 1 :(得分:1)

使用for...in迭代对象的所有键。因为forEach是一个数组方法,而userMap是一个对象,所以它不能使用该方法。

for (const id in userMap) {
  console.log(userMap[id].email);
}

答案 2 :(得分:0)

这个单线应该可以解决问题 Object.entries(users).map(([key,value]) => value.email)

  • Object.entries(users)返回键和值数组的数组
  • .map(([key,value]) => value.email)将提取所有用户的电子邮件字段

答案 3 :(得分:0)

使用for...in loop

for (const key in array) {
    let obj = array[key];
    console.log(obj.email);
    }

答案 4 :(得分:0)

请注意,forEach方法仅适用于数组。因此,您可能想在此处使用for...in来迭代userMap对象中的所有键,然后访问email键。

一个工作示例:

const userMap = {
  "5def224170a3ec0d90dbb89a": {
    "_id": "5def224170a3ec0d90dbb89a",
    "username": "dhruv1999",
    "fullname": "dhruv parmar",
    "email": "dhruv@gmail.com",
    "phone": "7045691556",
    "password": "$2a$10$UOk8E4fbkkZvSRSzQqWK4eyJ2Z.qtL4D38thfhzkxJ1pSxSFn5fna",
    "__v": 0
  },
  "5def2747d135311a747a9660": {
    "_id": "5def2747d135311a747a9660",
    "username": "dhruv1998",
    "fullname": "parmar",
    "email": "dhruvkp@gmail.com",
    "phone": "704591556",
    "password": "$2a$10$px5SK.OpP7i2Ryvg.ko/OeIbuq9XDT3luTrIBloNFCTiImUJC1BIm",
    "__v": 0
  }
};

for (const id in userMap) {
  console.log(userMap[id].email);
}

答案 5 :(得分:0)

假设我们的对象的名称为object

let object={
  "5def224170a3ec0d90dbb89a": {
    "_id": "5def224170a3ec0d90dbb89a",
    "username": "dhruv1999",
    "fullname": "dhruv parmar",
    "email": "dhruv@gmail.com",
    "phone": "7045691556",
    "password": "$2a$10$UOk8E4fbkkZvSRSzQqWK4eyJ2Z.qtL4D38thfhzkxJ1pSxSFn5fna",
    "__v": 0
  },
  "5def2747d135311a747a9660": {
    "_id": "5def2747d135311a747a9660",
    "username": "dhruv1998",
    "fullname": "parmar",
    "email": "dhruvkp@gmail.com",
    "phone": "704591556",
    "password": "$2a$10$px5SK.OpP7i2Ryvg.ko/OeIbuq9XDT3luTrIBloNFCTiImUJC1BIm",
    "__v": 0
  }
}

for(let obj in object){
  console.log(object[obj].email)
}

For in loop给出object(即5def224170a3ec0d90dbb89a5def2747d135311a747a9660中存在的所有密钥,然后您就可以从其中访问电子邮件属性。

答案 6 :(得分:0)

一个工作示例:

let data = {
  "5def224170a3ec0d90dbb89a": {
    _id: "5def224170a3ec0d90dbb89a",
    username: "dhruv1999",
    fullname: "dhruv parmar",
    email: "dhruv@gmail.com",
    phone: "7045691556",
    password: "$2a$10$UOk8E4fbkkZvSRSzQqWK4eyJ2Z.qtL4D38thfhzkxJ1pSxSFn5fna",
    __v: 0
  },
  "5def2747d135311a747a9660": {
    _id: "5def2747d135311a747a9660",
    username: "dhruv1998",
    fullname: "parmar",
    email: "dhruvkp@gmail.com",
    phone: "704591556",
    password: "$2a$10$px5SK.OpP7i2Ryvg.ko/OeIbuq9XDT3luTrIBloNFCTiImUJC1BIm",
    __v: 0
  }
};

   

Object.keys(data).forEach(item => {
  console.log(data[item].email);  //you can access email here.
});
   

答案 7 :(得分:-1)

使用此代码=>

  var data = {
    "5def224170a3ec0d90dbb89a": {
        "_id": "5def224170a3ec0d90dbb89a",
        "username": "dhruv1999",
        "fullname": "dhruv parmar",
        "email": "dhruv@gmail.com",
        "phone": "7045691556",
        "password": "$2a$10$UOk8E4fbkkZvSRSzQqWK4eyJ2Z.qtL4D38thfhzkxJ1pSxSFn5fna",
        "__v": 0
    },
    "5def2747d135311a747a9660": {
        "_id": "5def2747d135311a747a9660",
        "username": "dhruv1998",
        "fullname": "parmar",
        "email": "dhruvkp@gmail.com",
        "phone": "704591556",
        "password": "$2a$10$px5SK.OpP7i2Ryvg.ko/OeIbuq9XDT3luTrIBloNFCTiImUJC1BIm",
        "__v": 0
    }
}


let allEmails = '';

Object.keys(data).map((item) => {

    allEmails = allEmails + data.item.email + ','

})
console.log(allEmails) // here you will get all emails 

尝试一下,它将绝对有效。