简体   繁体   English

如何在 javascript Object.assign 方法中使用所有函数参数

[英]How can I use all function arguments inside javascript Object.assign method

I am making a server using nodejs & express in which user can request some data and server send response.我正在使用nodejs & express 制作一个服务器,用户可以在其中请求一些数据和服务器发送响应。 But, the data is array and I want to send a json response to the user.但是,数据是数组,我想向用户发送一个 json 响应。 So, I used forEach() method of array and use Object.assign() , so that I can get object.因此,我使用了数组的forEach()方法并使用Object.assign() ,以便我可以获取对象。 But the problem is I cannot use 'index' argument of forEach() method while 'value' argument is properly getting used inside the callback function.但问题是我不能使用forEach()方法的 'index' 参数,而在回调函数中正确使用了 'value' 参数。 When I use only 'index' argument, then my code runs ok but I want to use both arguments at the same time.当我只使用 'index' 参数时,我的代码运行正常,但我想同时使用这两个参数。

route.get('/getGPX/:number', passport.authenticate('jwt', { session: false }), (req, res) => {
    gpx.find({ username: req.user.username }, (err, result) => {
        if (err) console.log(err);
        if (result) {
            if (result.length === 0) {
                res.end();
                console.log('ended')
            }
            else {
                var jsonRes = {};
                result.forEach((value, index) => {

I can use 'value' but not 'index' from the arguments我可以使用参数中的“值”但不能使用“索引”

                    jsonRes = Object.assign({ index: value.data }, jsonRes);
                })

                res.json({data: jsonRes});

            }
        }
    }) 

I even tried using global var , but even it's not working, how can I use index as well as value argument at the same time我什至尝试使用global var ,但即使它不起作用,我怎样才能同时使用 index 和 value 参数

What is the JSON structure that you want ?你想要的 JSON 结构是什么?

if you want a json like :如果你想要一个像这样的json:

{
  0: "my first value",
  1: "second"
}

You just miss [] around index, here you put 'index' as a key not the value of index.您只是错过了索引周围的[] ,在这里您将“索引”作为键而不是索引的值。 So you override the index key in each iteration.所以你在每次迭代中覆盖索引键。

Here is the code that use the value of index as a key in a json.这是使用 index 的值作为 json 中的键的代码。

jsonRes = Object.assign({[index]: value.data}, jsonRes)

See here for a working example with more examples : https://repl.it/@Benoit_Vasseur/nodejs-playground有关更多示例的工作示例,请参见此处: https : //repl.it/@Benoit_Vasseur/nodejs-playground

Object.assign mutates the left-most parameter. Object.assign改变了最左边的参数。 it does not produce a new object.它不会产生新对象。 Since you are passing a literal object every time the jsonRes is going to be the last result.由于每次 jsonRes 将成为最后一个结果时,您都会传递一个文字对象。

Put jsonRes in the left instead - Object.assign(jsonRes, {index: value.data})将 jsonRes 放在左侧 - Object.assign(jsonRes, {index: value.data})

You might want to use a simple reduce instead of forEach and Object.assign :您可能想要使用简单的reduce而不是forEachObject.assign

} else {
  var jsonRes = result.reduce((r, v, i) => {r[i] = v.data; return r}, {});
  res.json({data: jsonRes});
}

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

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