preface
In JS handwritten interview questions, there are a lot of arrays, the following to a variety of postures to remove weight and flattening it.
Array to heavy
Array deduplication is a basic question in the interview, but there is still a lot of JS knowledge to be mined from it.
Set is most commonly used in ES6
A Set object is a collection of values, and you can iterate over its elements in the order in which they were inserted. Elements in a Set occur only once, that is, the elements in a Set are unique.
function unique(arr) {
return Array.from(new Set(arr))
}
let arr = [1.1.'true'.'true'.true.true.15.15];
console.log(unique(arr))// [ 1, 'true', true, 15 ]
let unique2 = arr= > [...new Set(arr)]
console.log(unique2(arr));// [ 1, 'true', true, 15 ]
Copy the code
Double for loop
The outer loop element is compared while the inner loop is in place.
function unique(arr) {
for (var i = 0; i < arr.length; i++) {
for (var j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) { // The first is the same as the second, and the splice method deletes the second
arr.splice(j, 1); j--; }}}return arr;
}
let arr = [1.1.'true'.'true'.true.true.15.15];
console.log(unique(arr))// [ 1, 'true', true, 15 ]
Copy the code
Deweight indexOf or includes
When there is no element in the array, indexOf returns -1, pushing it into the new array.
function unique(arr) {
var array = [];
for (var i = 0; i < arr.length; i++) {
if (array .indexOf(arr[i]) === -1) {
array .push(arr[i])
}
}
return array;
}
Copy the code
The includes() method is used to determine whether a string contains a specified substring. Returns true if a matching string is found, false otherwise.
function unique(arr) {
var array = [];
for (var i = 0; i < arr.length; i++) {
if(! array.includes( arr[i]) ) { array .push(arr[i]) } }return array;
}
Copy the code
The filter to heavy
Return if the index of the element in the original array is equal to the current index value, otherwise return the current element
function unique(arr) {
var res = arr.filter(function(item, index, array) {
return array.indexOf(item) === index
})
return res
}
Copy the code
Array flattening
Array flattening is the process of turning a multidimensional array into a one-dimensional array.
Flat method
The flat() method creates a new array in which all of the subarray elements are recursively concatenated until the specified depth is reached
const arr1 = [1.2[3.4[5.6]]];
arr1.flat(2);
// [1, 2, 3, 4, 5, 6]
const arr2 = [1.2[3.4[5.6[7.8[9.10]]]]];
arr2.flat(Infinity);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Copy the code
ES6 extension operator
Es6’s extension operator can turn a two-dimensional array into a one-dimensional array, and if the ARR contains an array, the extension operator is used once until there is no more.
function flatten(arr) {
while (arr.some(item= > Array.isArray(item))) { arr = [].concat(... arr); }return arr;
}
Copy the code
ToString +split
Call the toString method of the array, turn the array into a string and then split it back into an array. Each item in a split array is a string, so a map method is used to traverse the array to convert each item to a numeric value.
function flatten(arr) {
console.log(arr.toString());/ / 6
console.log(arr.toString().split(', ')); //['1', '2', '3', '4', '5', '6']
return arr.toString().split(', ').map(function(item) {
return Number(item); })}let ary = [1[2[3[4.5]]].6];
console.log(flatten(ary)); // [1, 2, 3, 4, 5, 6]
Copy the code
Use the Join method to achieve similar results to the toString method.
recursive
Iterate over each item recursively, continuing if it is an array, concat if it is not an array.
function flatten(arr) {
var result = [];
for (var i = 0, len = arr.length; i < len; i++) {
if (Array.isArray(arr[i])) {
result = result.concat(flatten(arr[i]))
}
else {
result.push(arr[i])
}
}
return result;
}
Copy the code
Reduce method implementation
The reduce method takes a function as an accumulator, and each value in the array (from left to right) starts to shrink and eventually evaluates to a value.
function flatten(arr) {
return arr.reduce((result, item) = > {
return result.concat(Array.isArray(item) ? flatten(item) : item); } []); }let ary = [1[2[3[4.5]]].6];
console.log(flatten(ary)); // [1, 2, 3, 4, 5, 6]
Copy the code
conclusion
Array de-duplication and array flattening are easy questions to ask in an interview. Here we share the solutions for different poses. If you are interested, you can exchange more knowledge in the comments section below!