Arrays 如何在angular 7 typescript中合并2个数组

Arrays 如何在angular 7 typescript中合并2个数组,arrays,angular,typescript,angular7,array-push,Arrays,Angular,Typescript,Angular7,Array Push,在一个特定组件的angular 7项目中,我必须通过api从wp站点和dotnet站点获取数据。 我从wp api获得的数据为(控制台日志数据): 我从dotnet api获取的数据为(控制台日志数据): 现在我需要合并这两个数组,洗牌数组元素并显示它。 到目前为止我所做的: public merge_array : Array<{ }> = []; /* wp api */ this.accountService.getKeynote( wp_params ).then( ( re

在一个特定组件的angular 7项目中,我必须通过api从wp站点和dotnet站点获取数据。 我从wp api获得的数据为(控制台日志数据):

我从dotnet api获取的数据为(控制台日志数据):

现在我需要合并这两个数组,洗牌数组元素并显示它。 到目前为止我所做的:

public merge_array : Array<{ }> = [];
/* wp api */
this.accountService.getKeynote( wp_params ).then( ( response: any ) => {
    this.merge_array ? this.merge_array : [];
    this.wp_data_array = response.data;
    for ( let value of this.wp_data_array ) {
        this.merge_array.push( value );
    };
});
/* dot net api */
this.accountService.getConferences( params ).then( ( dotnetresponse: any ) => {
    if ( dotnetresponse.status == 'Ok' ) {
        this.merge_array ? this.merge_array : [];
        this.dotnet_data_array = dotnetresponse.conferences;
        for ( let value of this.dotnet_data_array ) {
            this.merge_array.push( value );
        };
    }
});
合并数组的长度为0。我无法解决这个问题。关于阵列初始化或阵列推送,我做错了什么。 欢迎提供任何帮助/建议。

简单的
对象。分配(目标、源)
应该可以做到这一点

文件在

要洗牌数组,请使用分解:


数组为空并不奇怪,因为承诺的返回是异步操作的。因此,在记录值时,两个数组都将为空

你需要做的是使用。根据文件,

all()方法返回一个承诺,当作为iterable传递的所有承诺都已解析或iterable不包含承诺时,该承诺将解析。它拒绝的理由是第一个拒绝的承诺

现在可以做的是使用Promise.all解析来自两个请求的承诺(您可以使用
console.log(response)
验证承诺是否确实已解析并返回值),然后使用合并两个数组。然后,我们将使用Laurens Holst提供的算法随机重新排列数组。这将为您提供得到的无序数组

const getKeynote = this.accountService.getKeynote(wp_params);
const getConferences = this.accountService.getConferences(params);

Promise.all([getKeynote, getConferences]).then(response => {
  //console.log(response);
  const merged = [...response[0], ...response[1]];
  const shuffleArray = (array) => {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
  };
  shuffleArray(merged);
  console.log(merged);
});

长度:7
-为什么得出结论是零<代码>数组(0)是原型。@jonrsharpe我做了console.log(this.merge\u Array.length),它让我觉得有数据。我需要数组长度,但无论如何我总是得到0?是吗?因为在您发布的输出中,我们可以看到它显示为7。@jonrsharpe是的,它确实显示,但当我控制台长度(console.log(this.merge\u array.length))时,它始终显示为0。除了0之外,我似乎无法获得确切的长度。它使我得到空数组。
public merge_array : Array<{ }> = [];
/* wp api */
this.accountService.getKeynote( wp_params ).then( ( response: any ) => {
    this.merge_array ? this.merge_array : [];
    this.wp_data_array = response.data;
    for ( let value of this.wp_data_array ) {
        this.merge_array.push( value );
    };
});
/* dot net api */
this.accountService.getConferences( params ).then( ( dotnetresponse: any ) => {
    if ( dotnetresponse.status == 'Ok' ) {
        this.merge_array ? this.merge_array : [];
        this.dotnet_data_array = dotnetresponse.conferences;
        for ( let value of this.dotnet_data_array ) {
            this.merge_array.push( value );
        };
    }
});
[]
0: {title: "Henk WJ Ovink", description: "Special Envoy for International Water Affairs, Kin…ands, and Sherpa to the High Level Panel on Water", slug: "http://siwidev.websearchpro.net/press/", image: "http://siwidev.websearchpro.net/wp-content/uploads/2019/03/636385501414087698.png", imageWidth: 1903, …}
1: {title: "Amina J. Mohammed", description: "Deputy Secretary-General United Nations", slug: "http://siwidev.websearchpro.net/community/", image: "http://siwidev.websearchpro.net/wp-content/uploads/2019/03/h20fddcc.jpg", imageWidth: 776, …}
2: {id: 8342, title: "Panaceas or painkillers – what role for sustainability assessment tools?", description: null, slug: "8342-panaceas-or-painkillers---what-role-for-sustainability-assessment-tools", image: "https://siwi.websearchpro.net/Content/ProposalReso…g-2019-8342-tn-img-2019-8605-iStock-500553193.jpg", …}
3: {id: 8380, title: "Inclusive Policy and Governance for Water and Sanitation ", description: null, slug: "8380-inclusive-policy-and-governance-for-water-and-sanitation", image: "https://siwi.websearchpro.net/Content/ProposalReso…019-8420-img-2019-8420-org-InkedPhoto IWRM_LI.jpg", …}
4: {id: 8464, title: "Cities4Forests: 50 cities commit to forests citing water benefits", description: null, slug: "8464-cities4forests-50-cities-commit-to-forests-citing-water-benefits", image: "https://siwi.websearchpro.net/Content/ProposalReso…464-tn-img-2019-8481-WESCA illegal FS dumping.jpg", …}
5: {id: 8474, title: "Urban water resiliency: a coordinated response from source to settlement ", description: null, slug: "8474-urban-water-resiliency-a-coordinated-response-from-source-to-settlement", image: "https://siwi.websearchpro.net/Content/ProposalResources/Image/Default/default-www-tn.jpg", …}
6: {id: 8526, title: "Including all: participatory approaches in water governance and programmes ", description: null, slug: "8526-including-all-participatory-approaches-in-water-governance-and-programmes", image: "https://siwi.websearchpro.net/Content/ProposalReso…ge/2019/Thumbnail/img-2019-8526-tn-Field trip.JPG", …}
length: 7
__proto__: Array(0)
for (let i = target.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [target[i], target[j]] = [target[j], target[i]];
}
const getKeynote = this.accountService.getKeynote(wp_params);
const getConferences = this.accountService.getConferences(params);

Promise.all([getKeynote, getConferences]).then(response => {
  //console.log(response);
  const merged = [...response[0], ...response[1]];
  const shuffleArray = (array) => {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
  };
  shuffleArray(merged);
  console.log(merged);
});