1

hey there i have a small question i have data base in firebase that look like that

{ 
 0001: {
      name : ....,
      image: .... 
     },
 0002: {
       name: .... , 
       image: ...
    }
}

i want to use it with flat list but when i use object.values to transfer data i lost my id and also object.entries didnt give me anything Object.values(data); there is any way to create an array from this data like that ?

 {id ,
  name ,
  image}

and put it in my flat list beacause i want when to click on a item open new windows and send that id the id should be the key number 0001 0002 ......

3 Answers 3

2

You can use reduce https://developer.mozilla.org/vi/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

Object.keys(data).reduce((result, key) => {
  return result.concat({id: key, ...data[key]})
}, [])
Sign up to request clarification or add additional context in comments.

Comments

1

another way if you need:

Object.values(data).map((obj, i) => ({ id: Object.keys(data)[i], ...obj}));

Comments

0

you can use Object.entries(data) will return an array of [key, value] pairs

var data = { 
 0001: {
      name : "",
      image: "" 
     },
 0002: {
       name: "" , 
       image: ""
    }
}

then try to call

Object.entries(data)

Comments

Your Answer

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