This article describes the traversal method of arrays and objects in JS. Share it for your reference, as follows:
1. Array traversal:
First define an array
arr=['snow','bran','king','nightking'];
1、for
Loop, you need to know the length of the array;
2、foreach
, no return value, you can not know the length of the array;
(function(ele,index){ (index); (ele) })
3、map
Functions, iterate through each element of the array and callback operations, need to return the value, and the return value forms a new array, and the original array remains unchanged;
var newarr=(function(i){ return "hello "+i }); (newarr)
4、filter
Function: Filter elements passing through conditions to form a new array, and the original array remains unchanged;
var newarr=(function(i){ return i == "bran" }); (newarr)
5、some
Function, iterates over whether there are functions that meet the conditions in the array, and returns a Boolean value;
var yy=(function(i){ return >4 }); (yy) //true
6、every
Function, traverses whether each element of the array meets the conditions, and returns a boolean value;
var xx=(function(i){ return >4 }); (xx) //false
7、reduce
Function, execute callback functions in turn for each element in the array
grammar:
(callback, initialValue)
[0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){ return previousValue + currentValue; });
callback
: Executes a function for each value in the array, including four parameters;
- previousValue: The value returned by the last callback, or the initial value provided (initialValue);
- currentValue: The value currently processed;
- index: The index of the current element in the array;
- array: The array that calls reduce;
- initialValue: As the first parameter to callback for the first time;
For example:
var total = [0, 1, 2, 3].reduce(function(a, b) { return a + b; }); // total == 6 var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) { return (b); }); // flattened is [0, 1, 2, 3, 4, 5]
To provideinitialValue
Words:
var total = [0, 1, 2, 3].reduce(function(a, b) { return a + b; },4); (total); //10
2. Object traversal
var obj={snow:1,bran:2,king:3,nightking:4}; for(let i in obj){ (i+','+obj[i]) }
in
It can also be used to traverse arrays, buti
Corresponding to the arraykey
value:
for(let i in arr){ (i+','+arr[i]) }
PS:Here I would like to recommend a JS array traversal method analysis and comparison tool for your reference:
Performance analysis and comparison tool for common online JS traversal methods:http://tools./aideddesign/js_bianli
For more information about JavaScript, readers who are interested in reading this site's special topic:Summary of JavaScript array operation skills》、《JavaScript traversal algorithm and skills summary》、《JavaScript object-oriented tutorial》、《Summary of JavaScript mathematical operations usage》、《Summary of JavaScript data structure and algorithm techniques"and"Summary of JavaScript Errors and Debugging Skills》
I hope this article will be helpful to everyone's JavaScript programming.