I want to display only the first element of the key in JSON array data as an alert in Javascript.

Asked 2 years ago, Updated 2 years ago, 92 views

I would like to display only the first element of the key in JSON array data in Javascript (AB and A are displayed in consecutive alerts), but the source below does not work.How should I correct it?Could you please let me know?

<!DOCTYPE html>
<html lang="ja">
<head>
  <metacharset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title> Ayeo</title>

  <link href="css/bootstrap.min.css"rel="stylesheet">
  <link type="text/css"rel="stylesheet" href="css/bootstrap-timepicker.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
  <script src="js/bootstrap.min.js"></script>
  <script type="text/javascript" src="js/bootstrap-timepicker.min.js"></script>

  <body>
   <script>
     varjson_data='{"alfa":["AB", "CD", "EF", "GH"], "hiragana":["a", "i", "u", "e"]}';
     vardata = JSON.parse(json_data);
     for (key in data) {
         alert(key[0]);
     }
    </script>
  </body>
</html>

javascript json

2022-09-29 22:27

1 Answers

alert(key[0]) to alert(data[key][0]Try changing it to .


2022-09-29 22:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.