如何使用for loop()获取数组的键和值?

时间:2017-03-14 06:32:44

标签: javascript arrays object for-loop

我的阵列在下面,

  "custominfo": [
    {
      "fb": "www.fb.com"
    },
    {
      "google": "www.google.com"
    }
  ],

我想要fb,即密钥和www.fb.com,即使用for循环的值。我试过这个,

for(var i = 0;i< custominfo.length;i++){
     console.log(i[0])
}

打印,

{"fb":"www.fb.com"}

但我怎样才能获得关键和价值。感谢。

3 个答案:

答案 0 :(得分:1)

试试这个:

custominfo[i]

要获取密钥,您可以循环for (var k in custominfo[i]) { console.log('key:' + k); console.log('val:' + custominfo[i][k]); }

var appId = ConfigurationManager.AppSettings["ActiveDirectoryApplicationId"];
var appKey = ConfigurationManager.AppSettings["ActiveDirectoryApplicationKey"];
var aadInstance = ConfigurationManager.AppSettings["ActiveDirectoryInstance"];
var tenant = ConfigurationManager.AppSettings["ActiveDirectoryTenant"];
var onboardingResourceId = ConfigurationManager.AppSettings["OnboardingApplicationResourceId"];

var clientCredential = new ClientCredential(appId, appKey);
var bootstrapContext =
            ClaimsPrincipal.Current.Identities.First().BootstrapContext as
                System.IdentityModel.Tokens.BootstrapContext;
var userName = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn) != null ? ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn).Value : ClaimsPrincipal.Current.FindFirst(ClaimTypes.Email).Value;
var userAccessToken = bootstrapContext.Token;
var userAssertion = new UserAssertion(bootstrapContext.Token, "urn:ietf:params:oauth:grant-type:jwt-bearer", userName);
var authority = string.Format(System.Globalization.CultureInfo.InvariantCulture, aadInstance, tenant);

var userId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
var authContext = new AuthenticationContext(authority, new TokenCache());

var result = await authContext.AcquireTokenAsync(onboardingResourceId, clientCredential, userAssertion);
var accessToken = result.AccessToken;
return accessToken;

答案 1 :(得分:0)

var custominfo = [
    {
      "fb": "www.fb.com"
    },
    {
      "google": "www.google.com"
    }
  ]

for(var i = 0;i < custominfo.length;i++){
     console.log("Key: "+Object.keys(custominfo[i]));

     console.log("Value: "+ Object.values(custominfo[i]));
}

答案 2 :(得分:0)

尝试以下解决方案。将 <?php if(!isset($_POST['username'])||!isset($_POST['email'])||!isset($_POST['password'])){ die("Data Error"); } if(strlen($_POST['username'])>150){ die("Username too Long"); } if(!strpos($_POST['email'],"@")){ die("Invalid Email Address"); } include_once "config.php"; // congig.php is as follows //define("SERVER_NAME","localhost"); //define("USERNAME","root"); //define("PASSWORD",""); //define("DATABASE","mydatabase"); $conn = new mysqli(SERVER_NAME,USERNAME,PASSWORD,DATABASE); if($conn->connect_error){ die("Connection Error!".$conn->connect_error); } $username = $_POST['username']; // Code Works till here $stmt = $conn->prepare("SELECT count(*) FROM 'users' WHERE 'username' = ?"); $stmt->bind_param("s",$username); $stmt->execute(); $stmt->bind_result($count); $stmt->fetch(); if($count>0){ die("Username Already Exist"); } $stmt->close(); $email = $_POST['email']; $password = password_hash($_POST['password'],PASSWORD_BCRYPT); $stmt2 = $conn->prepare("INSERT INTO 'users' ('username','email','password') VALUES (?,?,?)"); if (!$stmt) { echo "false"; } else { $stmt2->bind_param("sss",$username,$email,$password); $stmt2->execute(); } $conn->close(); header("Location ../signup_success.php"); 用于loop数组,将另一个用于每个对象的键,如果每个对象包含多个键,则可以正常使用。

custominfo