简体   繁体   English

如何访问数组内的变量

[英]How to access variables inside an array

So, I have been trying to solve this all of yesterday and today but cannot figure it out. 因此,我一直在努力解决昨天和今天的所有问题,但无法解决。 I have the following returned in a variable called 我在名为的变量中返回了以下内容

var myRequest = req.body
console.log(myRequest) 

Produces the following: 产生以下内容:

{
    "methodcall": {
        "methodname": ["userLogin"],
        "params": [{
            "param": [{
                "value": [{
                    "string": ["test1"]
                }]
            }, {
                "value": [{
                    "string": ["password"]
                }]
            }]
        }]
    }
}

Now, I need to access the params key, and access the first param value so that whatever is returned in the first param value string is stored as username in a variable, and whatever is returned in param value string (the second one), is stored as a password. 现在,我需要访问params键,并访问第一个param值,以便将第一个param值字符串中返回的任何内容作为用户名存储在变量中,并将在param值字符串中(第二个)返回的所有内容存储为存储为密码。

So the final effect something like this: 所以最终的效果是这样的:

var username = myRequest.methodcall.params.param...first value string
var password = myRequest.methodcall.params.param...second value string

However, I am really struggling to understand how to do this. 但是,我真的很难理解该如何做。 Im guessing forEach loops would come in this, however I do not have experience with them so would appreciate some tips. 我猜想forEach循环会出现在其中,但是我没有使用它们的经验,因此希望您了解一些技巧。

Also, when I try doing myRequest.methodcall , I keep getting undefined returned. 另外,当我尝试执行myRequest.methodcall ,我总是返回未定义的返回值。

Any help will be greatly appreciated! 任何帮助将不胜感激!

What you have posted is JSON. 您发布的是JSON。 you need to set it up like: 您需要将其设置为:

var myRequest = JSON.parse(req.body)

this will allow you to access the it like a normal js object. 这将允许您像普通的js对象一样访问它。

It sounds like your value is in JSON, parse it first and then you should presumably be able to get its values: 听起来您的值在JSON中,请先解析它,然后大概应该能够获取其值:

var myRequest = JSON.parse(req.body);

var userName = myRequest.methodcall.params[0].param[0].value[0].string[0];
var password = myRequest.methodcall.params[0].param[1].value[0].string[0];

Use . 使用. to access keys in object and [] to access index in array. 访问对象中的键, []访问数组中的索引。

This code should work: 此代码应工作:

var username = myRequest.methodcall.params[0].param[0].value[0].string[0]

If you would like to use a loop at get the values test1 , password , and so on. 如果您想在处使用循环,请获取值test1password等。 You can use a loop and access the param array: 您可以使用循环并访问param数组:

var params = myRequest.methodcall.params[0].param;

params.forEach(function(item){
    console.log(item.value[0].string[0]);
});

Fiddle 小提琴

 //var myRequest = JSON.parse(req.body) // if JSON var myRequest = { "methodcall": { "methodname": ["userLogin"], "params": [{ "param": [{ "value": [{ "string": ["test1"] }] }, { "value": [{ "string": ["password"] }] }] }] } }; var params = myRequest.methodcall.params; var uname, pwd; for (var i = 0; i < params.length; i++) { console.log("uname is " + params[i].param[0].value[0].string); console.log("pwd is " + params[i].param[1].value[0].string) } 

if your response structure will be same then no need to go for loop or something, just directly access the username and password from response. 如果您的响应结构相同,则无需进行循环等操作,只需直接从响应中访问用户名和密码即可。 try this. 尝试这个。

var username = myRequest.methodcall.params[0].param[0].value[0].string[0];
var password = myRequest.methodcall.params[0].param[1].value[0].string[0];

Did you debug your code? 您是否调试了代码? I mean these code: 我的意思是这些代码:

var username = myRequest.methodcall.params.param[0];
var password = myRequest.methodcall.params.param[1];

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

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