-1

On my realtime firebase database I store my products with unique keys, i.e.,

- products
 |
  - KPYGghMerx_AKU8b4ki
   |
    - name: "product A"
    - price: 1.99
  - KPYNtki7UWnh5evYQgjT
   |
    - name: "product B"
    - price: 3.99

I retrieve this data as follows

this.productsRef.on('value', (snapshot) => {
  var data = snapshot.val();
}

This returns the object as expected, i.e.,

{
  KPYGghMerx_AKU8b4ki: {
                         name: "product A"
                         price: 1.99
                       },
  KPYNtki7UWnh5evYQgjT: {
                         name: "product B"
                         price: 3.99
                       }
 }

What's the easiest way to map these objects into an array without the unique keys? e.g.

{
  name: "product A"
  price: 1.99
},
{
  name: "product B"
  price: 3.99
}
2
  • Object.values({...data...}). Commented Aug 19, 2016 at 15:49
  • Object.keys(object).map(k=>object[k) Commented Aug 19, 2016 at 15:49

1 Answer 1

0

Object.values will extract the values from an object and return it as an array:

Object.values({ k1: v1, k2: v2 }); //[v1, v2]

If you need to polyfill it you can write a simple values function:

function values(obj) {
  return Object.keys(obj).map(function (key) {
    return obj[key];
  });
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.