define

An array is a set of values arranged in order. Each value position is numbered (starting at 0), and the entire array is represented by square brackets.

Any type of data can be put into an array. If the elements of an array are still arrays, you have a multidimensional array.

var a = [[1.2], [3.4]];
a[0] [1] / / 2
a[1] [1] / / 4
Copy the code

The nature of arrays

Essentially, an array is a special kind of object. The Typeof operator returns an array of type Object.

typeof [1.2.3] // "object"
Copy the code

An array is special in that its key names are a sequence of integers (0,1,2…) .

var arr = ['a'.'b'.'c'];

Object.keys(arr)
/ / / "0", "1", "2"]
Copy the code

In the above code, the object. keys method returns all the key names of the array. You can see that the array keys are integers 0, 1, 2.

Since the key names of array members are fixed (default is always 0, 1, 2…) , so arrays do not have to specify key names for each element, whereas objects must specify key names for each member. The JavaScript language dictates that the key name of an object is a string, so the key name of an array is a string. It can be read numerically because key names that are not strings are converted to strings.

var arr = ['a'.'b'.'c'];

arr['0'] // 'a'
arr[0] // 'a'
Copy the code

The code above uses a numeric value and a string as the key name, respectively, and the result is to read the array. The reason is that numeric key names are automatically converted to strings.

Note that this is also true when assigning. A value is always converted to a string and then assigned as a key name.

var a = [];

a[1.00] = 6;
a[1] / / 6
Copy the code

In the code above, since 1.00 is converted to a string of 1, the value can be read using the numeric key 1.

As mentioned in the previous chapter, objects have two ways of reading members: point structures (object.key) and square brackets (object[key]). However, you cannot use dot structures for numeric key names.

var arr = [1.2.3];
arr. 0 // SyntaxError
Copy the code

In the above code, arr.0 is not written legally because a single numeric value cannot be used as an identifier. So, array members can only be represented by square brackets arr[0] (square brackets are operators that accept numeric values)

Length attribute

The length property of an array, which returns the number of members of the array.

['a'.'b'.'c'].length / / 3
Copy the code

As long as it’s an array, it must have a length property. This property is a dynamic value equal to the largest integer in the key name plus 1.

var arr = ['a'.'b'];
arr.length / / 2

arr[2] = 'c';
arr.length / / 3

arr[9] = 'd';
arr.length / / 10

arr[1000] = 'e';
arr.length / / 1001
Copy the code

The code above shows that the array’s numeric keys do not need to be consecutive, and the value of the length attribute is always 1 greater than the largest integer key. In addition, this shows that arrays are dynamic data structures that can be added or subtracted at any time.

The length attribute is writable. If you artificially set a value less than the current number of members, the number of members in the array is automatically reduced to the value set by length.

var arr = [ 'a'.'b'.'c' ];
arr.length / / 3

arr.length = 2;
arr // ["a", "b"]
Copy the code

The above code indicates that when the length property of the array is set to 2 (i.e., the largest integer key can only be 1) then the integer key 2 (c) is no longer in the array and is automatically removed.

An effective way to empty an array is to set the length property to 0.

var arr = [ 'a'.'b'.'c' ];

arr.length = 0;
arr / / []
Copy the code

If length is artificially greater than the current number of elements, the number of members of the array increases to this value, and the new positions are empty.

var a = ['a'];

a.length = 3;
a[1] // undefined
Copy the code

The above code shows that when length is set to greater than the number of arrays, undefined is returned for reading new positions.

JavaScript will report an error if length is set to an invalid value.

// Set a negative value
[].length = -1
// RangeError: Invalid array length

// The number of elements in the array is greater than or equal to 2 ^ 32
[].length = Math.pow(2.32)
// RangeError: Invalid array length

// Sets the string
[].length = 'abc'
// RangeError: Invalid array length
Copy the code

It’s worth noting that since an array is essentially an object, you can add attributes to an array, but that doesn’t affect the value of the Length attribute.

var arr = [];
arr[-1] = 'a';
arr[Math.pow(2.32)] = 'b';

arr.length / / 0
arr[-1] // "a"
arr[4294967296] // "b"
Copy the code

In the code above, we added two invalid numeric keys to the array ARr, resulting in the length property unchanged. These numeric keys become string keys. The last two lines get values because the numeric key is converted to a string by default.

The in operator.

The operator in, which checks for the presence of a key name, applies to objects as well as arrays.

var arr = [ 'a'.'b'.'c' ];
2 in arr  // true
'2' in arr // true
4 in arr // false
Copy the code

The above code shows that the array has a key named 2. Since the key names are all strings, the value 2 is automatically converted to a string.

Note that the in operator returns false if a position in the array is empty.

var arr = [];
arr[100] = 'a';

100 in arr // true
1 in arr // false

Copy the code

In the above code, array arr has only one member arr[100], and all other key names return false.

The vacancy of an array

When there is an empty element somewhere in an array, that is, there is no value between two commas, the array is said to have a hole.

var a = [1.1];
a.length / / 3
Copy the code

The above code shows that the empty space of the array does not affect the length property.

Note that if the last element is followed by a comma, no space is created. In other words, the result is the same with or without the comma.

var a = [1.2.3,];

a.length / / 3
a / / [1, 2, 3]
Copy the code

In the above code, the last member of the array is followed by a comma. This does not affect the value of the length property, as it would without the comma.

The empty space of the array is readable, returning undefined.

var a = [, , ,];
a[1] // undefined
Copy the code

Using the delete command to delete an array member creates a void and does not affect the length attribute

var a = [1.2.3];
delete a[1];

a[1] // undefined
a.length / / 3
Copy the code

The code above deletes the second element of the array with the delete command. This position is empty, but has no effect on the length attribute. That is, the length attribute does not filter for vacancies. So be very careful when you use the Length attribute to traverse an array.

There’s a difference between an array where a position is empty and an array where a position is undefined. If it is empty, use the array’s forEach method, for… The in structure and the object. keys method are traversed, and the empty space is skipped.

var a = [, , ,];

a.forEach(function (x, i) {
  console.log(i + '. ' + x);
})
// No output is generated

for (var i in a) {
  console.log(i);
}
// No output is generated

Object.keys(a)
/ / []
Copy the code

If a position is undefined, it will not be skipped when traversing.

var a = [undefined.undefined.undefined];

a.forEach(function (x, i) {
  console.log(i + '. ' + x);
});
// 0. undefined
// 1. undefined
// 2. undefined

for (var i in a) {
  console.log(i);
}
/ / 0
/ / 1
/ / 2

Object.keys(a)
/ / [' 0 ', '1', '2')
Copy the code

That is, a void means that the array doesn’t have that element, so it won’t be iterated over, whereas undefined means that the array has that element, so it won’t be skipped over.

An array-like object

If all the keys of an object are positive integers or zero and have the length attribute, the object is much like an array and is syntactically called an array-like object.

var obj = {
  0: 'a'.1: 'b'.2: 'c'.length: 3
};

obj[0] // 'a'
obj[1] // 'b'
obj.length / / 3
obj.push('d') // TypeError: obj.push is not a function
Copy the code

In the above code, the object obj is an array-like object. However, “array-like objects” are not arrays because they do not have array-specific methods. The object obj does not have an array push method, and using that method raises an error.

The essential feature of “array-like objects” is the length attribute. As long as you have the Length attribute, you can think of this object as resembling an array. One problem, however, is that this length attribute is not a dynamic value and does not change as members change.

var obj = {
  length: 0
};
obj[3] = 'd';
obj.length / / 0
Copy the code

The above code adds a numeric key to the object obj, but the length property remains the same. So that tells us that OBj is not an array.

Typical “array-like objects” are the arguments objects for functions, as well as most DOM element sets, and strings.

/ / the arguments object
function args() { return arguments }
var arrayLike = args('a'.'b');

arrayLike[0] // 'a'
arrayLike.length / / 2
arrayLike instanceof Array // false

// Set of DOM elements
var elts = document.getElementsByTagName('h3');
elts.length / / 3
elts instanceof Array // false

/ / string
'abc'[1] // 'b'
'abc'.length / / 3
'abc' instanceof Array // false
Copy the code

The code above contains three examples, none of which are arrays (the instanceof operator returns false), but all of which look very much like arrays.

The slice method of arrays turns “array-like objects” into real arrays.

var arr = Array.prototype.slice.call(arrayLike);
Copy the code

Instead of converting to a real array, another way to use array methods is to call() and place the array’s methods on the object.

function print(value, index) {
  console.log(index + ':' + value);
}

Array.prototype.forEach.call(arrayLike, print);
Copy the code

In the above code, arrayLike represents an array-like object. ForEach () is not allowed, but forEach() can be grafted onto arrayLike with call().

The following example calls the forEach method on the Arguments object using this method

/ / the forEach method
function logArgs() {
  Array.prototype.forEach.call(arguments.function (elem, i) {
    console.log(i + '. ' + elem);
  });
}

// Equivalent to the for loop
function logArgs() {
  for (var i = 0; i < arguments.length; i++) {
    console.log(i + '. ' + arguments[i]); }}Copy the code

A similar string Array object, so you can also use Array. Prototype. ForEach. Call traversal.

Array.prototype.forEach.call('abc'.function (chr) {
  console.log(chr);
});
// a
// b
// c
Copy the code

Note that this method is slower than using forEach native to arrays, so it’s best to first convert “array-like objects” to real arrays, and then call the array’s forEach method directly.

var arr = Array.prototype.slice.call('abc');
arr.forEach(function (chr) {
  console.log(chr);
});
// a
// b
// c
Copy the code

Array

The constructor

Array is a JavaScript native object that is also a constructor that you can use to generate new arrays.

var arr = new Array(2);
arr.length / / 2
arr // [ empty x 2 ]
Copy the code

In the code above, argument 2 to the Array() constructor means that an Array of two members is generated, with null values at each position.

If the new keyword is not used, the result is the same.

var arr = Array(2);
/ / is equivalent to
var arr = new Array(2);
Copy the code

For semantic reasons, and in keeping with other constructor uses, it is recommended to always add new.

The Array() constructor has a major drawback that different numbers of arguments can lead to inconsistent behavior.

// Returns an empty array with no arguments
new Array(a)/ / []

// A single positive integer argument representing the length of the new array to be returned
new Array(1) // [ empty ]
new Array(2) // [ empty x 2 ]

// An error is reported when a non-positive integer value is used as a parameter
new Array(3.2) // RangeError: Invalid array length
new Array(-3) // RangeError: Invalid array length

// a single non-numeric value (such as string, Boolean, object, etc.) as an argument,
// The parameter is a member of the new array returned
new Array('abc') // ['abc']
new Array([1]) // [Array[1]]

// In the case of multiple arguments, all arguments are members of the new array returned
new Array(1.2) / / [1, 2]
new Array('a'.'b'.'c') // ['a', 'b', 'c']
Copy the code

As you can see, Array() behaves quite inconsistently as a constructor. Therefore, it is not recommended to use it to generate new arrays; it is better to use array literals directly.

A static method

Array.isArray()

The array. isArray method returns a Boolean value indicating whether the argument is an Array. It compensates for the typeof operator.

var arr = [1.2.3];

typeof arr // "object"
Array.isArray(arr) // true
Copy the code

In the above code, the Typeof operator can only show that an Array is of type Object, whereas the array. isArray method recognizes arrays.

Instance methods

The valueOf (), toString ()

The valueOf method is a method owned by all objects and represents an evaluation of that object. ValueOf methods vary from object to object; valueOf methods on arrays return the array itself.

var arr = [1.2.3];
arr.valueOf() / / [1, 2, 3]
Copy the code

The toString method is also generic for objects. The toString method of an array returns the string form of an array.

var arr = [1.2.3];
arr.toString() / / "1, 2, 3"

var arr = [1.2.3[4.5.6]];
arr.toString() 6 "/ /"
Copy the code

Push () and pop ()

The push method is used to add one or more elements to the end of an array and return the length of the array after the new elements are added. Note that this method changes the original array.

var arr = [];

arr.push(1) / / 1
arr.push('a') / / 2
arr.push(true, {}) / / 4
arr // [1, 'a', true, {}]
Copy the code

The code above adds four members to the array using the push method.

The pop method is used to remove the last element of the array and return it. Note that this method changes the original array.

var arr = ['a'.'b'.'c'];

arr.pop() // 'c'
arr // ['a', 'b']
Copy the code

Using the pop method on an empty array does not return an error, but returns undefined.

[].pop() // undefined
Copy the code

The shift (), unshift ()

The shift() method is used to remove the first element of the array and return that element. Note that this method changes the original array.

var a = ['a'.'b'.'c'];

a.shift() // 'a'
a // ['b', 'c']
Copy the code

In the code above, the array is changed by using the Shift () method.

The unshift() method is used to add an element to the first position of the array and returns the length of the array after the new element is added. Note that this method changes the original array.

var a = ['a'.'b'.'c'];

a.unshift('x'); / / 4
a // ['x', 'a', 'b', 'c']
Copy the code

The unshift() method can take multiple arguments, which are added to the target array header.

var arr = [ 'c'.'d' ];
arr.unshift('a'.'b') / / 4
arr // [ 'a', 'b', 'c', 'd' ]
Copy the code

join()

The join() method returns all array members concatenated as a string, using the specified parameter as the delimiter. If no arguments are provided, they are separated by commas by default.

var a = [1.2.3.4];

a.join(' ') // '1 2 3 4'
a.join('|') / / "1 | 2 | 3 | 4." "
a.join() / / "1, 2, 3, 4"
Copy the code

If the array member is undefined or null or empty, it is converted to an empty string.

[undefined.null].join(The '#')
/ / '#'

['a'.'b'].join(The '-')
// 'a--b'
Copy the code

concat()

The concat method is used to merge multiple arrays. It adds the new array member to the back of the original array member, and returns a new array, unchanged.

['hello'].concat(['world'])
// ["hello", "world"]

['hello'].concat(['world'], ['! '])
// ["hello", "world", "!"]

[].concat({a: 1}, {b: 2})
// [{ a: 1 }, { b: 2 }]

[2].concat({a: 1})
// [2, {a: 1}]
Copy the code

In addition to arrays as arguments, concat also accepts other types of values as arguments to be added to the end of the target array.

[1.2.3].concat(4.5.6)
// [1, 2, 3, 4, 5, 6]
Copy the code

If the array members contain objects, the concat method returns a shallow copy of the current array. A “shallow copy” means that the new array copies references to objects.

var obj = { a: 1 };
var oldArray = [obj];

var newArray = oldArray.concat();

obj.a = 2;
newArray[0].a / / 2
Copy the code

In the above code, the original array contains an object, and the new array generated by the concat method contains a reference to this object. So, when you change the original object, the new array changes.

reverse()

The Reverse method is used to reverse array elements and return the changed array. Note that this method changes the original array.

var a = ['a'.'b'.'c'];

a.reverse() // ["c", "b", "a"]
a // ["c", "b", "a"]
Copy the code

slice()

The slice() method extracts a portion of the target array and returns a new array, unchanged.

arr.slice(start, end);
Copy the code

Its first argument is the start position (starting at 0 and included in the new array returned), and its second argument is the end position (but the element at that position itself is not included). If you omit the second argument, you are returned to the last member of the original array.

var a = ['a'.'b'.'c'];

a.slice(0) // ["a", "b", "c"]
a.slice(1) // ["b", "c"]
a.slice(1.2) // ["b"]
a.slice(2.6) // ["c"]
a.slice() // ["a", "b", "c"]
Copy the code

The last example in the above code, slice(), takes no arguments and essentially returns a copy of the original array.

If the argument to the slice() method is negative, it indicates where the reciprocal is evaluated.

var a = ['a'.'b'.'c'];
a.slice(-2) // ["b", "c"]
a.slice(-2, -1) // ["b"]
Copy the code

In the code above, -2 represents the second position in the reciprocal calculation, and -1 represents the first position in the reciprocal calculation.

If the first argument is greater than or equal to the length of the array, or if the second argument is less than the first argument, the empty array is returned.

var a = ['a'.'b'.'c'];
a.slice(4) / / []
a.slice(2.1) / / []
Copy the code

An important use of the slice() method is to turn array-like objects into real arrays.

Array.prototype.slice.call({ 0: 'a'.1: 'b'.length: 2 })
// ['a', 'b']

Array.prototype.slice.call(document.querySelectorAll("div"));
Array.prototype.slice.call(arguments);
Copy the code

None of the arguments in the above code are arrays, but they can be turned into real arrays by calling the slice() method on them through the call method

splice()

The splice() method is used to delete a portion of the original array and add a new array member at the deleted location, returning the deleted element. Note that this method changes the original array.

arr.splice(start, count, addElement1, addElement2, ...) ;Copy the code

The first argument to splice is the starting position of the deletion (starting from 0), and the second argument is the number of elements to be deleted. If there are more arguments, these are the new elements to be inserted into the array.

var a = ['a'.'b'.'c'.'d'.'e'.'f'];
a.splice(4.2) // ["e", "f"]
a // ["a", "b", "c", "d"]
Copy the code

The code above removes two array members from position 4.

map

The map() method passes all the members of the array to the argument function in turn and returns the result of each execution as a new array.

var numbers = [1.2.3];

numbers.map(function (n) {
  return n + 1;
});
/ / [2, 3, 4]

numbers
/ / [1, 2, 3]
Copy the code

In the code above, all the members of the numbers array execute the argument function, and the result is returned as a new array, unchanged.

The map() method takes a function as an argument. When this function is called, the map() method passes it three arguments: the current member, the current location, and the array itself.

[1.2.3].map(function(elem, index, arr) {
  return elem * index;
});
/ / [0, 2, 6]
Copy the code

In the code above, the map() method callback takes three arguments: elem is the value of the current member, index is the location of the current member, and arr is the original array ([1, 2, 3]).

If the array is empty, the map() callback does not execute at that location and skips the array empty.

var f = function (n) { return 'a' };

[1.undefined.2].map(f) // ["a", "a", "a"]
[1.null.2].map(f) // ["a", "a", "a"]
[1.2].map(f) // ["a", , "a"]
Copy the code

In the code above, the map() method does not skip undefined and NULL, but does skip empty Spaces.

forEach()

The forEach() method is similar to the map() method in that it takes arguments to each member of the array. However, the forEach() method does not return a value and is only used to manipulate data. That is, if the purpose of array traversal is to get a return value, use the map() method, otherwise use the forEach() method.

ForEach () is used in the same way as the map() method, and the argument is a function that again takes three arguments: the current value, the current position, and the entire array.

function log(element, index, array) {
  console.log('[' + index + '] = ' + element);
}

[2.5.9].forEach(log);
/ / [0] = 2
/ / [1] = 5
/ / [2] = 9
Copy the code

In the code above, forEach() traverses the array not to get a return value, but to print something on the screen, so the map() method is unnecessary.

Note that the forEach() method cannot interrupt execution and always iterates through all members. If you want to break traversal when certain conditions are met, use a for loop.

The forEach() method also skips arrays of empty Spaces.

var log = function (n) {
  console.log(n + 1);
};

[1.undefined.2].forEach(log)
/ / 2
// NaN
/ / 3

[1.null.2].forEach(log)
/ / 2
/ / 1
/ / 3

[1.2].forEach(log)
/ / 2
/ / 3
Copy the code

In the code above, the forEach() method does not skip undefined and null, but does skip empty space.

filter()

The filter() method is used to filter the members of an array, and the members that meet the criteria are returned as a new array.

It takes a function that all array members execute in turn, returning true members as a new array. This method does not change the original array.

[1.2.3.4.5].filter(function (elem) {
  return (elem > 3);
})
/ / [4, 5]
Copy the code

The above code returns an array member greater than 3 as a new array.

var arr = [0.1.'a'.false];

arr.filter(Boolean)
// [1, "a"]
Copy the code

In the code above, the filter() method returns all members of the array arr whose Boolean value is true.

The parameter function of the filter() method can take three arguments: the current member, the current position, and the entire array.

[1.2.3.4.5].filter(function (elem, index, arr) {
  return index % 2= = =0;
});
/ / [1, 3, 5]
Copy the code

The above code returns a new array of even-numbered members.

Some (), every ()

These methods, like Assert, return a Boolean value that determines whether an array member meets a condition.

They take a function as an argument that all array members execute in turn. This function takes three arguments: the current member, the current location, and the entire array, and then returns a Boolean value.

The some method returns true as long as one member returns true, and false otherwise.

var arr = [1.2.3.4.5];
arr.some(function (elem, index, arr) {
  return elem >= 3;
});
// true
Copy the code

In the above code, some returns true if arr has a member greater than or equal to 3.

The every method returns true for all members, and false otherwise.

var arr = [1.2.3.4.5];
arr.every(function (elem, index, arr) {
  return elem >= 3;
});
// false
Copy the code

In the above code, not all arr members are greater than or equal to 3, so return false.

Note that for an empty array, some returns false, every returns true, and neither callback is executed.

function isEven(x) { return x % 2= = =0 }

[].some(isEven) // false
[].every(isEven) // true
Copy the code

The some and every methods can also take a second argument that binds the this variable inside the argument function.

Reduce (), reduceRight ()

The Reduce and reduceRight methods process each member of the array in turn, eventually accumulating to a value. The difference is that reduce is processed from left to right (first member to last member), reduceRight is processed from right to left (last member to first member), and everything else is exactly the same.

[1.2.3.4.5].reduce(function (a, b) {
  console.log(a, b);
  return a + b;
})
/ / 1. 2
/ / 3. 3
4 / / 6
/ / 10 5
// Final result: 15
Copy the code

In the code above, the reduce method evaluates the sum of all the members of the array. On the first execution, a is the first member 1 of the array, and B is the second member 2 of the array. On the second execution, a is the return value 3 from the previous round, and b is the third member 3. For the third time, a is the return value of the previous round 6, and b is the fourth member 4. For the fourth time, a is the return value of the previous round 10, and b is the fifth member 5. At this point all members are iterated, and the return value of the entire method is the return value of the last round, 15.

The first parameter of the Reduce and reduceRight methods is a function. The function takes the following four arguments.

  1. Cumulative variable, which defaults to the first member of the array
  2. Current variable, which defaults to the second member of the array
  3. Current position (starting from 0)
  4. The original array

Of these four parameters, only the first two are required; the last two are optional.

If you want to specify an initial value for a cumulative variable, you can place it in the second argument to the Reduce and reduceRight methods.

[1.2.3.4.5].reduce(function (a, b) {
  return a + b;
}, 10);
/ / 25
Copy the code

The code above specifies that the initial value of parameter A is 10, so the array starts at 10 and the final result is 25. Notice that B is iterating from the first member of the array.

The second argument above sets the default value and is especially useful when dealing with empty arrays.

function add(prev, cur) {
  return prev + cur;
}

[].reduce(add)
// TypeError: Reduce of empty array with no initial value
[].reduce(add, 1)
/ / 1
Copy the code

In the code above, the reduce method returns an error because the empty array cannot get its initial value. In this case, adding the second argument ensures that a value is always returned.

Here is an example of the reduceRight method.

function subtract(prev, cur) {
  return prev - cur;
}

[3.2.1].reduce(subtract) / / 0
[3.2.1].reduceRight(subtract) / / - 4
Copy the code

In the code above, the reduce method is equivalent to 3 minus 2 and then minus 1, and the reduceRight method is equivalent to 1 minus 2 and then minus 3.

Because these two methods iterate over a set of numbers, they can actually be used to do some traversal related operations. For example, find the array member with the longest character length.

function findLongest(entries) {
  return entries.reduce(function (longest, entry) {
    return entry.length > longest.length ? entry : longest;
  }, ' ');
}

findLongest(['aaa'.'bb'.'c']) // "aaa"
Copy the code

In the code above, the reduce argument function takes the longer array member as the cumulative value. This results in the cumulative value being the member with the longest character length after iterating through all members.

IndexOf (), lastIndexOf ()

The indexOf method returns the position of the first occurrence of a given element in the array, or -1 if none occurs.

var a = ['a'.'b'.'c'];

a.indexOf('b') / / 1
a.indexOf('y') // -1
Copy the code

The indexOf method can also accept a second parameter, indicating the starting point of the search.

['a'.'b'.'c'].indexOf('a'.1) // -1
Copy the code

The code above searches for character A starting at position 1, and the result is -1, which means no search is found.

The lastIndexOf method returns the last occurrence of a given element in the array, or -1 if none occurred.

var a = [2.5.9.2];
a.lastIndexOf(2) / / 3
a.lastIndexOf(7) // -1
Copy the code

Note that these two methods cannot be used to search for the location of NaN, that is, they cannot determine whether an array member contains a NaN.

[NaN].indexOf(NaN) // -1
[NaN].lastIndexOf(NaN) // -1
Copy the code

This is because internally, the two methods are compared using the strict equality operator (===), and NaN is the only value that is not equal to itself.

The chain using

Many of these array methods return arrays, so they can be chained.

var users = [
  {name: 'tom'.email: '[email protected]'},
  {name: 'peter'.email: '[email protected]'}]; users .map(function (user) {
  return user.email;
})
.filter(function (email) {
  return /^t/.test(email);
})
.forEach(function (email) {
  console.log(email);
});
// "[email protected]"
Copy the code

In the above code, generate an array of all Email addresses, filter out the Email addresses starting with t, and print it out.