Home>
Often use javascript to operate on arrays and json arrays,But after a long time, I forgot to do it.So simply record here
Look at the code directly
<! Doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript array operations</title>
<script type="application/javascript" src="lib/jquery.min.js"></script>
</head>
<body>
<h2>javascript operation array</h2>
<input type="button" value="jsArray" "jsarrays ();">
<br>
<input type="button" value="jsonarray1" "jsonarrays ();">
</body>
<script type="application/javascript">
//Array operations
function jsarrays () {
//create a javascript array
var a=new array ();
a.push ("a");
a.push ("b");
a.push ("c");
//Add three elements to the array
alert (a);
//Use indexof to indicate where the element is in the array
alert ("a index of a" + a.indexof ("a"));
//Use jquery to facilitate the array
$(a) .each (function (index, row) {
alert ("index is" + index);
alert ("row is" + row);
})
}
//json array operation
function jsonarrays () {
//First also create a json array
var a=new array ();
a.push ({a:1});
a.push ({a:2});
a.push ({a:3});
//Similarly add three elements to the array
//The result of alert is [{object, object}, {object, object}, {object, object}]
alert ("a is" + a);
//Use the json method to convert the json array into a string. At this time, the alert is a json string.
alert (json.stringify (a));
//Get the value of the first element of the json array directly,At this point know that the key of json is a
alert (a [0] .a);
//Iterate over the key of the first element of the json array. At this time, the result of alert can be a, and the corresponding value can be obtained.
for (var key in a [1]) {
alert ("key is" + key);
alert ("value is" + a [0] [key]);
}
//Through json array with jquery
$(a) .each (function (index, row) {
for (var key in row) {
alert ("each key is" + key);
alert ("each value is" + row [key]);
}
})
}
</script>
</html>
Related articles
- Method for converting array in JS to JSON format string
- Javascript object json array json string mutual conversion and value method
- JS to json format string as an object or array (front and back)
- Methods to use JSON and restore json to array in PHP language
- Simple implementation of json object and array and conversion into js object
- JS simple loop through the json array method
- How to add json data to js array and the difference between js array and json
- JSON usage of PHP array to JS array, how JS receives PHP array
- 3 ways for JQuery to traverse json array
Trends