如何在angular 7打字稿中合并2个数组

禅定

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

(2) [{…}, {…}]
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, …}
length: 2
__proto__: Array(0)

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

(5) [{…}, {…}, {…}, {…}, {…}]
0: {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", …}
1: {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", …}
2: {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", …}
3: {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", …}
4: {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: 5
__proto__: Array(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 );
        };
    }
});

但是当我在这里控制台日志“merge_array”时,结果是:

[]
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)

合并数组的长度为 0。我无法解决这个问题。我在数组初始化或数组推送方面做错了什么。欢迎任何帮助/建议。

wentjun

数组为空也就不足为奇了,因为 Promise 的返回是异步操作的。因此,当您记录值时,两个数组都将为空。

你需要做的是使用Promise.all根据文档,

Promise.all() 方法返回一个 Promise,当所有作为可迭代传递的承诺都已解决或可迭代不包含承诺时,该 Promise 会解决。它以第一个拒绝的承诺的原因拒绝。

现在可以做的是使用 Promise.all 来解决来自两个请求的承诺(您可以使用console.log(response)来验证承诺确实已解决并返回一个值),然后使用扩展语法合并两个数组。然后,我们会随机洗牌使用提供了该算法的阵列在这里被劳伦斯霍尔斯特。这应该给你结果洗牌数组。

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);
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Angular 2打字稿@injectable

来自分类Dev

如何在Angular2 RC5打字稿中调用共享指令的方法?

来自分类Dev

如何在Angular 2打字稿中生成md5哈希?

来自分类Dev

如何在Angular 2打字稿中生成md5哈希?

来自分类Dev

如何在打字稿中动态合并两个数组对象?

来自分类Dev

如何在打字稿中动态合并两个数组对象?

来自分类Dev

Angular 2打字稿:TypeError:this.validator不是一个函数

来自分类Dev

Angular 2打字稿调用JavaScript函数

来自分类Dev

组件未渲染Angular 2打字稿

来自分类Dev

Angular2打字稿拖放功能

来自分类Dev

Angular 2打字稿调用JavaScript函数

来自分类Dev

angular 2打字稿中的成员属性和构造函数的语法

来自分类Dev

何时在Angular2打字稿中创建构造函数?

来自分类Dev

Angular 2打字稿中@Inject和@Injectable有什么区别

来自分类Dev

如何将小叶路由机包含到angular 2打字稿webpack应用程序中

来自分类Dev

在另一个导航组件中使用Angular2打字稿组件

来自分类Dev

如何使用另一个数组中的项目过滤打字稿数组

来自分类Dev

带有Angular 2打字稿的Visual Studio代码中的重复标识符错误

来自分类Dev

带有Angular 2打字稿的Visual Studio代码中的重复标识符错误

来自分类Dev

什么是angular2打字稿和angular2 ecma脚本

来自分类Dev

如何在angular.js 2.0打字稿应用程序中使用Google登录按钮?

来自分类Dev

在打字稿中合并两个数组后如何获得唯一元素?

来自分类Dev

解析器错误:表达式的意外结尾:{{value?}} | Angular 2打字稿

来自分类Dev

在angular2打字稿方法中指定返回类型

来自分类Dev

angular 2打字稿不能在环境上下文中声明实现

来自分类Dev

在Angular 2打字稿应用程序中使用moment.js

来自分类Dev

扩展http类并访问自定义属性(Angular2打字稿)

来自分类Dev

解析器错误:表达式的意外结尾:{{value?}} | Angular 2打字稿

来自分类Dev

设置angular2打字稿环境时遇到问题

Related 相关文章

  1. 1

    Angular 2打字稿@injectable

  2. 2

    如何在Angular2 RC5打字稿中调用共享指令的方法?

  3. 3

    如何在Angular 2打字稿中生成md5哈希?

  4. 4

    如何在Angular 2打字稿中生成md5哈希?

  5. 5

    如何在打字稿中动态合并两个数组对象?

  6. 6

    如何在打字稿中动态合并两个数组对象?

  7. 7

    Angular 2打字稿:TypeError:this.validator不是一个函数

  8. 8

    Angular 2打字稿调用JavaScript函数

  9. 9

    组件未渲染Angular 2打字稿

  10. 10

    Angular2打字稿拖放功能

  11. 11

    Angular 2打字稿调用JavaScript函数

  12. 12

    angular 2打字稿中的成员属性和构造函数的语法

  13. 13

    何时在Angular2打字稿中创建构造函数?

  14. 14

    Angular 2打字稿中@Inject和@Injectable有什么区别

  15. 15

    如何将小叶路由机包含到angular 2打字稿webpack应用程序中

  16. 16

    在另一个导航组件中使用Angular2打字稿组件

  17. 17

    如何使用另一个数组中的项目过滤打字稿数组

  18. 18

    带有Angular 2打字稿的Visual Studio代码中的重复标识符错误

  19. 19

    带有Angular 2打字稿的Visual Studio代码中的重复标识符错误

  20. 20

    什么是angular2打字稿和angular2 ecma脚本

  21. 21

    如何在angular.js 2.0打字稿应用程序中使用Google登录按钮?

  22. 22

    在打字稿中合并两个数组后如何获得唯一元素?

  23. 23

    解析器错误:表达式的意外结尾:{{value?}} | Angular 2打字稿

  24. 24

    在angular2打字稿方法中指定返回类型

  25. 25

    angular 2打字稿不能在环境上下文中声明实现

  26. 26

    在Angular 2打字稿应用程序中使用moment.js

  27. 27

    扩展http类并访问自定义属性(Angular2打字稿)

  28. 28

    解析器错误:表达式的意外结尾:{{value?}} | Angular 2打字稿

  29. 29

    设置angular2打字稿环境时遇到问题

热门标签

归档