Angular - [] .concat.apply返回空数组

时间:2017-02-23 00:32:25

标签: javascript angularjs json ionic-framework angular-promise

我有这个离子代码:

.factory('resultsFactory', function($http, $q, $rootScope, $firebaseArray, $timeout) { 
  var results = {};  

  function _all(){
    var d = $q.defer();
    var palabras =  $rootScope.textAreaOfrecer.toLowerCase().split(' ');
    var solicitudes = [];
    Promise.all(
              solicitudes = palabras.map(palabra => $firebaseArray(firebase.database().ref().child("solCompras").orderByChild("palabras/" 
                                               + palabra).equalTo(true) ) )
                     );

    var array = [].concat.apply([],solicitudes);

    d.resolve(array);

    return d.promise;       
  }

  results.all = _all;
  return results;
})

问题是代码:var array = [] .concat.apply([],solicitudes);返回一个空数组。

json of solicitudes

2 个答案:

答案 0 :(得分:0)

改为使用[].concat.apply(solicitudes)。可能palabras是一个空数组,然后[].map(...) == []可以直接返回Promise.all(solicitudes)

function _all() {
        var palabras = $rootScope.textAreaOfrecer.toLowerCase().split(' ');
        return Promise.all(palabras.map((palabra) => {
            return $firebaseArray(firebase.database().ref().child("solCompras").orderByChild("palabras/" + palabra).equalTo(true));
        }));
    }

答案 1 :(得分:0)

$scope.solicitudes = [{post: 'Loading..'}];


Promise.all(
      $scope.solicitudes = palabras.map(palabra => $firebaseArray(firebase.database().ref().child("solCompras").orderByChild("palabras/" + palabra
                                                   ).equalTo(true) ) )
             ).then( function (){
                               //http://www.jstips.co/en/flattening-multidimensional-arrays-in-javascript/
                               var myNewArray = $scope.solicitudes.reduce(function(prev, curr) {
                                 return prev.concat(curr);
                               });

                               $scope.solicitudes = myNewArray;
                         });