JavaScript - 将两个JSON请求合并到一个Object中

Jon*_*a33 1 javascript ajax jquery json http

如何将两个不同的JSON响应组合到一个对象中,以便我可以从单个源处理我的数据?

我已经阅读了不同的实现,但不是我现在编写代码的方式.

我很漂亮,所以如果你能给我一个动手实例,我将非常感激.

例如:

$(function() {
    let hotels = 'data1.json';
    let guests = 'data2.json';
    $.getJSON(hotels)
        .then(function (res) {
            console.log(res);
        })
        .fail(function (err) {
            console.log(err);
        });
    $.getJSON(guests)
        .then(function (res) {
            console.log(res);
        })
        .fail(function (err) {
            console.log(err);
        });

      /*** start logic for application here ***/  

    let newObject = //How can I combine both responses above???
    console.log(newObject);


});
Run Code Online (Sandbox Code Playgroud)

同意我从每个请求得到的回复: 在此输入图像描述

Bar*_*uch 6

您可以执行Promise.all链接以在新对象中获取两个结果.

Promise.all([$.getJSON(hotels), $.getJSON(guests)])
  .then( results => {
    console.log(results);
  });
Run Code Online (Sandbox Code Playgroud)

示例:https://repl.it/Ls2c/1