user11583965
user11583965

Reputation:

how can i trasfere data object to an array

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 ......

Upvotes: 1

Views: 52

Answers (3)

Roy.B
Roy.B

Reputation: 2106

another way if you need:

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

Upvotes: 1

Ashish Kamble
Ashish Kamble

Reputation: 2627

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)

Upvotes: 0

Giang Le
Giang Le

Reputation: 7054

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]})
}, [])

Upvotes: 2

Related Questions