简体   繁体   English

$$key php 等效于 node.js

[英]$$key php equivalent in node.js

I have an array and I am using foreach loop to iterate through that array and I want each key in that array to be converted in to a varible.我有一个数组,我正在使用 foreach 循环遍历该数组,并且我希望将该数组中的每个键转换为一个变量。 like we do in PHP.就像我们在 PHP 中所做的那样。 for example:例如:

foreach (['browser_id', 'device_id', 'os_id', 'event', ] as $var) {
            $$var = $request->$var ?? Session::put($var) ?? "";
        }

How to do exactly this in Node.js?如何在 Node.js 中做到这一点? I have this array我有这个数组

var paramss = ['browser_id', 'device_id', 'os_id', 'event', 'eventName', 'billingstatus', 'step'];

I want to loop these using foreach and convert each of these keys in to a varible.我想使用 foreach 循环这些键并将这些键中的每一个转换为变量。 like browser_id,device_id etc will be a varible.Please help像 browser_id、device_id 等将是一个变量。请帮助

I have tried this:我试过这个:

paramss.forEach(element => {
  var element = req.query[element] ? req.query[element] : '';
console.log(element) });

window object is not available in NodeJS however you can use global window object 在NodeJS中不可用,但是您可以使用全局

global["foo"] = 'bar'

So you can access it using foo variable所以你可以使用foo变量来访问它

console.log(foo) // bar

so in your case you can do this所以在你的情况下你可以这样做

paramss.forEach(element => {
  global[element] = req.query[element] ? req.query[element] : '';
})

then you can get the element as variable defined然后您可以将元素作为变量定义

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

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