Array. The from () and Array. The reduce ()

The answer:

The array.from () method converts an array-like object or traversable object into a real Array

The array.reduce () method applies a function to each element (from left to right) in the accumulator and Array, reducing it to a single value.

Resolution:

Array.from()

// What is an array-like object? The basic requirement for an array-like object is an object with a length attribute. ArrayLike = {0: "Tom ", 1: "65", 2:" male ", 3: [" Jane ", "John ", "Mary"], length: 4}; let arr = Array.from(arrayLike); console.log(arr); / / [' Tom ', '65' and 'male', [' Jane ', 'John', 'Mary']] / / so, if the above code length attribute to remove? As it turns out, the answer will be an empty array of length 0. Let arrayLike = {name: "Tom ", age: "65", sex:" male ", friends: "Tom ", age: "65", sex:" male ", friends: ["jane", "john", "Mary"], length: 4 }; let arr = Array.from(arrayLike); console.log(arr); // An array-like object can be converted to a real array only if the following conditions are met: The array object must have the length attribute, which specifies the length of the array. If there is no length attribute, the transformed array is an empty array. // the property name of the array object must be a numeric or string number. Let arr = [12, 45, 97, 9797, 564, 134, 45642]; let arr = [12, 45, 97, 9797, 564, 134, 45642]; let set = new Set(arr); console.log(Array.from(set)); [12, 45, 97, 9797, 564, 134, 45642] // Array.from can also take a second argument, similar to the map method of arrays, which is used to process each element and place the processed value into the returned Array. Let arr = [12, 45, 97, 9797, 564, 134, 45642]; let set = new Set(arr); console.log(Array.from(set, item => item + 1)); [13, 46, 98, 9798, 565, 135, 45643] // 3, convert string to array: let STR = "Hello world! ; console.log(Array.from(str)); // ["h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d", "!"] Log (array. from([12, 45, 47, 56, 213, 4654, 154])); In this case, array. from returns an identical new ArrayCopy the code

reference

Array.reduce()

Array. reduce(function(Accumulator, currentValue, currentIndex, array), initialValue); Accumulator: An accumulator that is the return value of the last call to the function. The first time for the initialValue | | arr [0] currentValue: the value of the function being processed in the array. The first time the initialValue | | arr [1] currentIndex: data is processing element in the index, if provided the initialValue, starting from 0. Otherwise, start from 1. Array: array to call reduce initialValue: optional, initialValue of the accumulator. If no, the accumulator's first value is currentValue; Note: An error is reported when the reduce method is called on an empty array with no initial value set. // No initial value [1, 2, 3, 4]. Reduce (function(Accumulator, currentValue, currentIndex, array) { return accumulator + currentValue; }); / / 10Copy the code
callback accumulator currentValue currentIndex array return value
first call 1(first element of array) 2(array second element) 1(none initial value is 1) [1, 2, 3, 4] 3
second call 3 3 2 [1, 2, 3, 4] 6
third call 6 4 3 [1, 2, 3, 4] 10
// With an initial value [1, 2, 3, 4]. Reduce (function(Accumulator, currentValue, array) { return accumulator + currentValue; }, 10); / / 20Copy the code
callback accumulator currentValue currentIndex array return value
first call 10(initial value) 1(first element of array) 0(with an initial value of 0) [1, 2, 3, 4] 11
second call 11 2 1 [1, 2, 3, 4] 13
third call 13 3 2 [1, 2, 3, 4] 16
fourth call 16 4 3 [1, 2, 3, 4] 20
//1. Sum array elements [1, 2, 3, 4]. Reduce ((a, b) => a + b); 10 / / / / 2. The two dimensional array into a one-dimensional array [[1, 2], [3, 4], [5, 6]], the reduce ((a, b) = > a.c oncat (b), []) / / [1, 2, 3, 4, 5, Reduce ((items, item) => {if (item in items) {items[item]++; } else { items[item] = 1; } return items; }, {}) //{1: 2, 2: 2, 3: 2, 4: 1} [/ / array to heavy (1) (1, 2, 3, 1, 2, 3, 4, 4, 5)], reduce ((init, current) => { if (init.length === 0 || init.indexOf(current) === -1) { init.push(current); } return init; }, []) / / [1, 2, 3, 4, 5] [/ / array and (2) (1, 2, 3, 1, 2, 3, 4, 4, 5)]. The sort (). Reduce ((init, current) => { if (init.length === 0 || init[init.length - 1] ! == current) { init.push(current); } return init; } []); //[1, 2, 3, 4, 5]Copy the code