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

How can i get all the email such as [email protected], [email protected]

I tried doing but not getting the email.

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

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

      });
3
  • 1
    is userMap the object itself? Commented Dec 17, 2019 at 6:33
  • Can you explain more about your object? Commented Dec 17, 2019 at 6:35
  • Is this a Map object? Commented Dec 17, 2019 at 6:36

8 Answers 8

2

The reason why the following doesn't work is because userMap is an object (you're also not closing your outer .forEach loop, which will be a syntax error) :

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

In order to use .forEach(), you need to use it on an array (or an array-like collection). Instead, you can get the Object.values() of your object. Object.values() gives back an array, so you can then use .map() on it to get the email properties:

const obj = {"5def224170a3ec0d90dbb89a": { "_id": "5def224170a3ec0d90dbb89a", "username": "dhruv1999", "fullname": "dhruv parmar", "email": "[email protected]", "phone": "7045691556", "password": "$2a$10$UOk8E4fbkkZvSRSzQqWK4eyJ2Z.qtL4D38thfhzkxJ1pSxSFn5fna", "__v": 0 }, "5def2747d135311a747a9660": { "_id": "5def2747d135311a747a9660", "username": "dhruv1998", "fullname": "parmar", "email": "[email protected]", "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

Sign up to request clarification or add additional context in comments.

Comments

1

Use for...in to iterate all keys of the object. Because forEach is an array method and userMap is an object so it can't use the method.

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

Comments

1

Assuming the name of our object is object.

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

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

For in loop gives all keys present in your object i.e 5def224170a3ec0d90dbb89a and 5def2747d135311a747a9660 and from there you can access email property.

Comments

0

This one-liner should do the trick Object.entries(users).map(([key,value]) => value.email)

  • Object.entries(users) returns an array of key and value array
  • .map(([key,value]) => value.email) will extract email field from all users

Comments

0

using for...in loop

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

Comments

0

Please note that forEach method is available for Arrays only. Hence you may want to use for...in here to iterate all the keys in the userMap object and then access the email key.

A working example:

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

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

Comments

0

A working example:

let data = {
  "5def224170a3ec0d90dbb89a": {
    _id: "5def224170a3ec0d90dbb89a",
    username: "dhruv1999",
    fullname: "dhruv parmar",
    email: "[email protected]",
    phone: "7045691556",
    password: "$2a$10$UOk8E4fbkkZvSRSzQqWK4eyJ2Z.qtL4D38thfhzkxJ1pSxSFn5fna",
    __v: 0
  },
  "5def2747d135311a747a9660": {
    _id: "5def2747d135311a747a9660",
    username: "dhruv1998",
    fullname: "parmar",
    email: "[email protected]",
    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.
});
   

Comments

-1

Use this code =>

  var data = {
    "5def224170a3ec0d90dbb89a": {
        "_id": "5def224170a3ec0d90dbb89a",
        "username": "dhruv1999",
        "fullname": "dhruv parmar",
        "email": "[email protected]",
        "phone": "7045691556",
        "password": "$2a$10$UOk8E4fbkkZvSRSzQqWK4eyJ2Z.qtL4D38thfhzkxJ1pSxSFn5fna",
        "__v": 0
    },
    "5def2747d135311a747a9660": {
        "_id": "5def2747d135311a747a9660",
        "username": "dhruv1998",
        "fullname": "parmar",
        "email": "[email protected]",
        "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 

Try this it will work definitely.

4 Comments

I think you want to use Object.values(data) instead of Object.keys(data)
It should look like this let allEmails = Object.values(data).map(item => item.email).join(','); console.log(allEmails);
You were wrong because Object.values(data) returns array of keys and i want that for looping Object.values(data) returns values but i dont need it.
@RahulGupta no, qoomon is correct, item.email doesn't make any sense if item is the key because then it will just be a string, and no property .email exists on a string. It makes sense if it is the value as that would be the object, where .email access the email property. Also currently, you're using .map() without using its return value. .map() isn't intended to be used this way. Instead, you can use .forEach() if all you intend on doing is iterating and causing side-effects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.