2

What I had

{
     "1zkhj45kjb3h3jj27777fjd": {"-L7hgtdyYUYY56":{ email: "[email protected]",name:"abc"   } },
     "1zkhj45kjb3h898fj7fjddk": {"-L7hgtdyYUYY56":{ email: "[email protected]",name:"dumy" } } 
}

What I've done

const snapshot = snap.val()
const items = Object.values(snapshot)

now items looks like

[
    {"-L7hgtdyYUYY56":{ email: "[email protected]",name:"abc"   } },
    {"-L7hgtdyYUYY56":{ email: "[email protected]",name:"dumy" } } 
]

But I want

[
    { email: "[email protected]",name:"abc"   } ,
    { email: "[email protected]",name:"dumy" } 
]

I have tried all other stretegies like Object.keys, Object.enteries. if I again call object.values it gives same result.

How to do it javascript ? I'm newbie to react native and javascript.

Thank you!

3 Answers 3

4

Use Array.flatMap() with Object.values() to get an array of the inner objects:

const items = {
     "1zkhj45kjb3h3jj27777fjd": {"-L7hgtdyYUYY56":{ email: "[email protected]",name:"abc"   } },
     "1zkhj45kjb3h898fj7fjddk": {"-L7hgtdyYUYY56":{ email: "[email protected]",name:"dumy" } } 
}

const result = Object.values(items).flatMap(Object.values)
  
console.log(result)

If Array.flatMap() is not supported, use Array.map() instead. However, the result would be an array of arrays, so you'll need to flatten it. You can flatten it by spreading the array of arrays into Array.concat():

const items = {
     "1zkhj45kjb3h3jj27777fjd": {"-L7hgtdyYUYY56":{ email: "[email protected]",name:"abc"   } },
     "1zkhj45kjb3h898fj7fjddk": {"-L7hgtdyYUYY56":{ email: "[email protected]",name:"dumy" } } 
}

const result = [].concat(...Object.values(items).map(Object.values))
  
console.log(result)

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

9 Comments

great answer @Ori Drori but will you please tell me that why this is giving error in react native (undefiled is not an function)
It's not supported by react native out of the box, and you'll need to use a shim.
I've added another solution that flattens the array using Array.concat().
Great Man and bundle of thanks for saving my day @Ori Drori can you please some give explaination to this. it will be realy helpful for me for next time
Cool, i'll get it by time Thanks for explanation Stay blessed
|
3

Use Object.values() twice with map() and flat():

const data = {
 "1zkhj45kjb3h3jj27777fjd": {"-L7hgtdyYUYY56":{ email: "[email protected]",name:"abc"   } },
 "1zkhj45kjb3h898fj7fjddk": {"-L7hgtdyYUYY56":{ email: "[email protected]",name:"dumy" } } 
};

const result = Object.values(data).map(Object.values).flat();

console.log(result);

1 Comment

Thanks for answer, unfortunately it didn't worked in react native
1

Use Object.values() to get the nested objects as an array, then use .map() to get the value of the -L7hgtdyYUYY56 of each of them.

const snapshot = {
 "1zkhj45kjb3h3jj27777fjd": {"-L7hgtdyYUYY56":{ email: "[email protected]",name:"abc"   } },
 "1zkhj45kjb3h898fj7fjddk": {"-L7hgtdyYUYY56":{ email: "[email protected]",name:"dumy" } } 
};

var result = Object.values(snapshot).map(e => e["-L7hgtdyYUYY56"]);
console.log(result);

This assumes that the nested objects just have one property and they're all the same property name, as in your example. If it's more dynamic, the other answers that flatten the objects are more appropriate.

2 Comments

@Bammer thanks for answer but what if i don't know -L7hgtdyYUYY56 this values each time. all answers snippets are working fine but in react native it is not working
If you don't know the property name, use the other answers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.