简体   繁体   English

如何将这些数据合并到一个对象中?

[英]How can I combine this data into one Object?

This one is a tricky one. 这是一个棘手的问题。

So, lets say I have two JS objects that are fetched via REST call, via two callbacks. 所以,可以说我有两个JS对象,它们是通过REST调用,通过两个回调来获取的。

So, we have: 因此,我们有:

call1() - POST method - parsed JSON to JS object, route: {{url}}/data call1() - POST方法-将JSON解析为JS对象,路由: {{url}}/data

call1.json: call1.json:

 "data": [
            { 
                "id": "1",
                "volume:" "2000"
            }, 
            { 
                "id": "2",
                "volume:" "3000"
            }, 
            { 
                "id": "3",
                "volume:" "4000"
            }, 
            { 
                "id": "4",
                "volume:" "5000"
            }
       ];

call2(req.body.id) - GET method - parsed JSON to JS object, route: {{url}}/data/:id call2(req.body.id) - GET方法-解析JSON来JS对象,路线{{url}}/data/:id

For example, if I pass req.body.id as = 1 got from the first response, it will open data for that id . 例如,如果我将req.body.id作为第一个响应的= 1传递,它将打开该id数据。 So: 所以:

return call2(2) will return the data from this JSON: call2.json: return call2(2)将通过以下JSON返回数据:call2.json:

"data": [
            { 
                "id": "1",
                "add_data": "important string",
                "add_data_2": "important string two"
            }, 
       ];

The bottom line is, when doing the {{url}}/data route call - call() I need to serve all the data from {{url}}/data/:id routes, and bind them to the correct id . 最重要的是,在进行{{url}}/data路由调用call()我需要提供{{url}}/data/:id路由中的所有数据,并将它们绑定到正确的id So, for example, the scenario I am trying to achieve is this: 因此,例如,我要实现的方案是这样的:

Inside call() : get call1.json: data, do as many call2(req.body.id) calls as there are ids in the first object and then combine add_data and add_data_two values in the first object. call()内部:获取call1.json:数据,执行与第一个对象中的ids一样多的call2(req.body.id)调用,然后在第一个对象中组合add_dataadd_data_two值。 So for example the final object would look like this. 因此,例如最终对象看起来像这样。

console.log(response)

"data": [
                { 
                    "id": "1",
                    "volume:" "2000",
                    "add_data": "important string",
                    "add_data_2": "important string two"
                }, 
                { 
                    "id": "2",
                    "volume:" "3000",
                    "add_data": "important string",
                    "add_data_2": "important string two"
                }, 
                { 
                    "id": "3",
                    "volume:" "4000",
                    "add_data": "important string",
                    "add_data_2": "important string two"
                }, 
                { 
                    "id": "4",
                    "volume:" "5000",
                    "add_data": "important string",
                    "add_data_2": "important string two"
                }
           ];

This is what I have tried so far: 到目前为止,这是我尝试过的:

async get_data(req) {
        try {
            const objFirst = await call1(); //gets data
            let objTwo = '';

            for (let i = 0; i < objFirst.data.length; i++) {
                objTwo = await call2({id: objFirst.data[i].id}) //gets data
            }
            return objFirst;
        } catch(err) {
            console.log("Error: ", err)
        }
    }

But it does not work. 但这行不通。 How can I get all data, and make as many as call2(id) as there are ids and combine that all in one object? 我怎样才能获得的所有数据,并进行多达call2(id)因为有ids并结合所有的在一个对象? Basically, I need to repeat this callback -> call2(id) as many ids we receive in call1() . 基本上,我需要repeat此回调-> call2(id)与我们在call1()收到的ids一样多。

Thanks, sorry if it looks like a mess. 谢谢,对不起,如果看起来一团糟。

You can use the map function and spread operator for this. 您可以为此使用map函数和散布运算符。 Something like below. 像下面这样。 Call2 function just simulates what an endpoint would return, but you get the idea. Call2函数只是模拟端点将返回的内容,但是您明白了。

var data = [
    {
        id: 1,
        add_data: "1111"
    },
    {
        id: 2,
        add_data: "2222"
    }
];

var data2 = [
    {
        id: 1,
        volume: "bla"
    },
    {
        id: 2,
        volume: "bla"
    }
];

function call2(id) {
    return data2.filter(x => x.id == id)[0];
}

var result = data.map(x => {
    var response = call2(x.id);
    return {
        ...x,
        ...response       
    }
})

console.dir(result[0]);

The speed of your solution (loop through an array and doing http calls to get more data is really slow). 解决方案的速度(遍历数组并执行http调用以获取更多数据的速度确实很慢)。 If you have a lot of these functions that needs to combine data from different datasources, and depending on your project size, i would look into either RXJS or GraphQL (If you really need performance). 如果您有很多这些功能需要合并来自不同数据源的数据,并且取决于您的项目规模,那么我将研究RXJS或GraphQL(如果您确实需要性能)。 RXJS have great functions to merge, combine, map etc data. RXJS具有强大的功能来合并,合并,映射等数据。

RXJS RXJS

GraphQL GraphQL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM