如何在javascript中将数组附加到现有数组

时间:2017-12-05 13:15:06

标签: javascript arrays

我需要对Sharepoint中的不同列表进行查询,每个列表都可以获得一些结果。

但是我不知道如何将一个结果附加到下一个,接下来看看修剪过的数据变量。

function GetData(billCycleId, clientCode, jobCodes, engagementCode) {
                    var enhanceFunctions = [
                        function(searchResultRow) {
                            return spService.AddHyperLinkOnFields(searchResultRow, config.HyperLinks);
                        },
                        function(searchResultRow) {
                            return spService.AddPresenceOnFields(searchResultRow, config.UserFields);
                        },
                        function(searchResultRow) {
                            return spService.FormatDateFields(searchResultRow, config.DateFields, generalConfig.DateTimeFormat);
                        },
                        function(searchResultRow) {
                            return spService.AddImageMapping(searchResultRow, config.ImageFields);
                        },
                        function(searchResultRow) {
                            return spService.FormatNumberFields(searchResultRow, config.NumberFields);
                        }
                    ];

                    var selectProperties = spService.TransformFieldsToSelectProperties(config.Fields); 
                    var extendedSelectProperties = selectProperties.slice(); // copy array
                    var hyperLinkedProperties = spService.TransformFieldsToSelectProperties(config.HyperLinks)
                    extendedSelectProperties = extendedSelectProperties.concat(hyperLinkedProperties);

                    spService.GetAllListsFromWeb()
                        .then(function(lists){
                            var listEnumerator = lists.getEnumerator();
                            while (listEnumerator.moveNext()) {
                                var oList = listEnumerator.get_current();
                                var title = oList.get_title();
                                var id = oList.get_id();
                                if(title.indexOf("Bill Cycles")){
                                    // Get data from SP
                                    GetRelatedBillCyclesFromList(listid, extendedSelectProperties, billCycleId, clientCode, jobCodes, engagementCode, enhanceFunctions)
                                    .then(function (data) {
                                        var trimmedData = spService.SpSearchQuery.TrimSearchResultsToSelectProperties(data, selectProperties);                          
                                        // Add data to dataTable

                                    })
                                    .catch (function (message) {
                                        vm.Name = "Error";
                                        vm.ValidDataLoaded = true;
                                    });
                                }
                                //Do something with oList.
                            }

                            var dataTable = $(tableSelector).DataTable();
                            dataTable.clear().rows.add(trimmedData).columns.adjust().draw(); // Resize columns based on new data sizes                                                      
                            vm.ValidDataLoaded = true;

                        })

                }



function getAllListsFromWeb(){  
                        var deferred = $q.defer();
                        var context = SP.ClientContext.get_current();
                        var web = context.get_web();
                        var lists = web.get_lists();                          
                        context.load(lists);
                        context.executeQueryAsync(
                           function() {
                                $log.info("Successfully retrieved list item result");                          
                                deferred.resolve(lists);
                           },
                           function(error, errorInfo) {
                                $log.warn("Retrieving list item result failed");

                                deferred.reject(errorInfo);
                           }
                        );
                        return deferred.promise;
                    }

更新1:

我收到以下错误: 无法读取未定义的属性“推送”

更新了相关代码:

spService.GetAllListsFromWeb()
                        .then(function(lists){
                            var listEnumerator = lists.getEnumerator();
                            var result;
                            while (listEnumerator.moveNext()) {
                                var oList = listEnumerator.get_current();
                                var title = oList.get_title();
                                var id = oList.get_id();
                                if(title.indexOf("Bill Cycles") !== -1){
                                    // Get data from SP
                                    GetRelatedBillCyclesFromList(id, extendedSelectProperties, billCycleId, clientCode, jobCodes, engagementCode, enhanceFunctions)
                                    .then(function (data) {
                                        var trimmedData = spService.SpSearchQuery.TrimSearchResultsToSelectProperties(data, selectProperties);                          
                                        // Add data to dataTable

                                        trimmedData.forEach(function(item){ // loop over source array
                                            result.push(item); //append to result array
                                        });
                                    })
                                    .catch (function (message) {
                                        vm.Name = "Error";
                                        vm.ValidDataLoaded = true;
                                    });
                                }
                                //Do something with oList.
                            }

                            var dataTable = $(tableSelector).DataTable();
                            dataTable.clear().rows.add(result).columns.adjust().draw(); // Resize columns based on new data sizes                                                       
                            vm.ValidDataLoaded = true;

                        })

1 个答案:

答案 0 :(得分:2)

您可以循环遍历数组并将每个项目添加到要追加的数组中。

let result = [];
...

trimmedData.forEach(function(item){ // loop over source array
  result.push(item); //append to result array
});
...