0
[
  {
    "first" : {
      "email" : "[email protected]"
    },
    "second" : {
      "email" : "[email protected]"
    }
  }
]

as you know first and second is inside index 0 but how can read them using map without using like

user.map(c=>c.first.email)
10
  • 4
    I think the structure of data you posted here is wrong. Arrays cannot have named keys, they only have numbered keys called indexes/indices Commented Apr 12, 2020 at 14:33
  • 2
    You can't as it is not a valid array. Commented Apr 12, 2020 at 14:34
  • this structure of data i got it from firebase not from me Commented Apr 12, 2020 at 14:34
  • this structure of data i got it from firebase not from me Commented Apr 12, 2020 at 14:36
  • This is neither a valid array nor a valid object. I think the first square brackets should be { instead of [ Commented Apr 12, 2020 at 14:38

2 Answers 2

1
let users = [
  {email: "[email protected]"},
  {email: "[email protected]"}
]

// .map() return new array

let user = users.map((item) => item.email)
console.log(user)
console.log(user[0])
console.log(user[1])
Sign up to request clarification or add additional context in comments.

Comments

0

It's no less clunky, but Object.keys() will achieve the same results

let obj = {
  "user": {
    "first": {
      "email": "[email protected]"
    },
    "second": {
      "email": "[email protected]"
    }
  }
}

let res = Object.keys(obj.user).map(el => {
  return obj.user[el].email
})

console.log(res)

4 Comments

@Aziz one problem I saw with your code it the outer array should have an object inside of it. first and second are keys, javascript arrays don't have keys.
it's from firebase not from me
@Aziz unfortunately it's not valid Javascript so it will always throw an error. You should research objects vs. arrays in javascript
{ "user":{ "first" : { "email" : "[email protected]" }, "second" : { "email" : "[email protected]" } } } it basically like that

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.