JavaScript的:无法合并两个数组 [英] JavaScript : unable to merge two arrays

查看:171
本文介绍了JavaScript的:无法合并两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是JavaScript的一个老兵,我有一个小问题:

I'm not a veteran of JavaScript and I have a little problem :

在一个AngularJS的控制器,我得到2阵列从形式 [{ID:1,名:aname1}的一个WebService,{ID:2 ,名:aname2}] 。它们同时具有相同的结构(这不应该是重要)。

In an AngularJS controller, I get 2 arrays from a WebService of the form [{"id":"1", "name":"aname1"}, {"id":"2", "name":"aname2"}]. They have both the same structure (this shouldn't be important).

使用 CONCAT()推()我无法将这些数组合并在一起,我不T明白为什么。

With concat() or push() I'm unable to merge these arrays together, and I don't understand why.

我试过

var arrayS = Service.list();           // Get data from WebService
var arrayAE = ActeurExterne.list();    // Idem
var arrayRes = arrayS.concat(arrayAE);
$scope.acteurs = arrayRes;

在我的AngularJS应用程序,数组 acteurs 是空的(如果我将其显示在 NG-重复循环外,它显示 [] 而数组和arrayAE显示其内容)

In my AngularJS app, the array acteurs is empty (if I displays it outside a ng-repeat loop, it displays [] while arrayS and arrayAE display their contents)

和相同的逻辑有:

array1.push.apply(array1, array2);

我试过一个在推数组2一个元素循环,同样的结果。

I tried to push elements of array2 one by one in a for loop, same result.

毫无效果。为什么呢?

推荐答案

您实际上是正确的合并阵列和这两种方法将工作(如他人提供的说,您使用的CONCAT <返回值/ code>),但结果是空的,因为你这样做的时候从Ajax请求的答案还没有到达,数组仍然是空的。

You're actually merging the arrays correctly and both methods would work (provided like others said that you use the return value of concat), but the result is empty because you're doing it when the answers from the ajax requests didn't arrive yet and the arrays are still empty.

在制作Ajax请求必须操纵成功回调函数内的结果,而不是在Ajax请求code同级别;例如:

When making ajax requests you must manipulate the result inside the success callback function, not at the same level of the ajax request code; for example

var myvec = [];
$.getjson(request_url, function(data) {
    myvec = data;
});
process(myvec);

就不行,因为它试图使用 myvec 内容的请求已提交之后,但答复的到来之前。正确的code是代替:

will NOT work, because it's attempting to use the content of myvec right after the request has been submitted but before the reply arrived. Correct code is instead:

$.getjson(request_url, function(data) {
    // This code is executed when the data arrives from the server
    process(data);
});

如果您需要连接两个异步查询的结果,您必须是链状他们(进行第二次查询只允许一次返回),或者你必须等待第二个完成:

If you need to concatenate the results of two async queries you must either chain them (make the second query only once the first returned) or you must wait for the second one to complete:

// First solution, simple but slower (waits for first reply
// before making second request)
$.getjson(request_url_1, function(data_1) {
    $.getjson(request_url_2, function(data_2) {
        process(data_1.concat(data_2));
    });
});

// Second solution; starts both requests and waits for both
// of them to complete before doing the processing

var count = 0;
var res = [];
function handle_data(data) {
    res = res.concat(data);
    count += 1;
    if (count == 2) {
        process(res);
    }
}

$.get(request_url_1, handle_data);
$.get(request_url_2, handle_data);

这疯狂的猜测是简单地从这种错误在初学code频率来了。

This crazy guess is simply coming from the frequency that this kind of error has in beginner code.

这篇关于JavaScript的:无法合并两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆