SoFunction
Updated on 2025-03-01

Summary of traversal method examples of arrays and objects in JS

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、forLoop, 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、mapFunctions, 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、filterFunction: 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、someFunction, 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、everyFunction, traverses whether each element of the array meets the conditions, and returns a boolean value;

var xx=(function(i){
  return >4
});
(xx)       //false

7、reduceFunction, 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 provideinitialValueWords:

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])
}

inIt can also be used to traverse arrays, butiCorresponding to the arraykeyvalue:

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 methodshttp://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.