如何使用js将多个数据数组从firebase合并到一个数组?

时间:2017-04-04 12:24:58

标签: javascript arrays firebase firebase-realtime-database

这里我从firebase数据库动态获取多个数组数据。我是这样的:

[Array(2)]
   0:"php",
   1:"-kyzencds1"
 [Array(2)]
   0:"html",
   1:"-kyzssencdsd"

如何合并为单个数组?

array[
    0:"php",
    1:"-kyzencds1",
    2:"html",
    3:"-kyzssencdsd"
   ]

以下是我的代码:

    var tags = myFirebased.ref().child('tags');
    tags.on("child_added", function(snapshot) {
        var tagkeys = snapshot.key;
        var tagName = snapshot.child("tagname").val();

     var myObj = {
        name: tagName,
        keys:tagkeys,
     };
    let array = [];
     var arr = Object.keys(myObj).map(function(k) { return myObj[k] });
     array.push(arr); 

      console.log(Array.prototype.concat(array));

1 个答案:

答案 0 :(得分:0)

https://lodash.com/docs/4.17.4#concat或者只是使用 var arrTotal=arr1.concat(arr2)

//例如添加了

var tags = myFirebased.ref().child('tags');
var allTags = ['php', 'kyzencds1'];
tags.on("child_added", function (snapshot) {
    //some actions
    var receivedTags = ["html", "-kyzssencdsd"]; // This new tags right ?
    if (Array.isArray(receivedTags) && receivedTags.length) {
        allTags = allTags.concat(receivedTags); 
    }
});