简体   繁体   中英

How to add array of elements in firebase?

I want know how to simply store the data from the array to firebase. Let us consider that I having an array of element.

function Something() {
    var elements=new Array()
    elements[0]=10;
    elements[1]=20;
    elements[2]=30;

    database = firebase.database();
    var ref = database.ref('ExTable');
    ref.set(elements);
}

Will the ref.set(elements); function will save the data to my firebase DB?

Now this array contains three values, and how can I store them to firebase as nodes below any parent node. Like

+---Parent
      |
      |
      +---0
      |   |
      |   |
      |   +---10
      |
      |
      +---1
      |   |
      |   |
      |   +---20
      |
      |
      +---2
          |
          |
          +---10

I want to store them as like in the above example. Because my array sometimes contains various number of elements.

Just pass it with array of objects like shown below.

var array = [];

array.push({"name":"Barry"});
array.push({"name":"Allen"});

ref.push(array);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM