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>
alert(key[0])
to alert(data[key][0]Try changing it to
.
© 2024 OneMinuteCode. All rights reserved.