Share some tips of JavaScript

  • 2021-10-16 00:58:22
  • OfStack

Array de-duplication

ES 6 provides several concise methods of array deduplication, but this method is not suitable for dealing with arrays of non-basic types. For primitive array deduplication, you can use... new Set () to filter out duplicate values in the array and create a new array with only one value.


const array = [1, 1, 2, 3, 5, 5, 1] 
const uniqueArray = [...new Set(array)]; 
console.log(uniqueArray); 

> Result:(4) [1, 2, 3, 5]

This is a new feature in ES6. Before ES6, we needed to use more code to achieve the same effect. This technique applies to arrays that contain basic types: undefined, null, boolean, string, and number. If the array contains an object, function, or other array, you need to use another method.

In addition to the above methods, you can also use Array. from (new Set ()):


const array = [1, 1, 2, 3, 5, 5, 1] 
Array.from(new Set(array)) 

> Result:(4) [1, 2, 3, 5]

In addition, it can be implemented using Array's. filter and indexOf ():


const array = [1, 1, 2, 3, 5, 5, 1] 
array.filter((arr, index) => array.indexOf(arr) === index) 

> Result:(4) [1, 2, 3, 5]

Note that the indexOf () method returns the first occurrence of the array item in the array. This is why we can compare the index returned by the indexOf () method with the current index in each iteration to determine if the current item is duplicate.

Ensure the length of the array

When working with the grid structure, if the length of each row of the original data is not equal, the data needs to be recreated. To ensure that the data length of each row is equal, you can use Array. fill to process


let array = Array(5).fill(''); 
console.log(array); 
> Result: (5) ["", "", "", "", ""]

Array mapping

Method that does not use Array. map to map array values.


const array = [ 
 { 
 name: ' Desert ', 
 email: 'w3cplus@hotmail.com' 
 }, 
 { 
 name: 'Airen', 
 email: 'airen@gmail.com'
 }] 
 const name = Array.from(array, ({ name }) => name) 
 
 > Result: (2) [" Desert ", "Airen"]

Array truncation

If you want to delete a value from the end of an array (delete the last item in the array), there is a faster alternative than using splice ().

For example, if you know the size of the original array, you can redefine the value of the length attribute of the array, and you can delete the value from the end of the array:


let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
console.log(array.length) 
> Result: 10 

array.length = 4 
console.log(array) 
> Result: (4) [0, 1, 2, 3]

This is a particularly concise solution. However, the slice () method runs faster and performs better:


let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; 
array = array.slice(0, 4); 
console.log(array); 

> Result: [0, 1, 2, 3]

Filter out falsy values from arrays

If you want to filter the falsy values in an array, such as 0, undefined, null, false, you can do this by using map and filter methods:


const array = [0, 1, '0', '1', ' Desert ', 'w3cplus.com', undefined, true, false, null, 'undefined', 'null', NaN, 'NaN', '1' + 0] 
array.map(item => { 
 return item 
}).filter(Boolean) 

> Result: (10) [1, "0", "1", " Desert ", "w3cplus.com", true, "undefined", "null", "NaN", "10"]

Gets the last item of the array

When the value of slice () of an array is positive, the items of the array are truncated from the beginning of the array, and if the value is a negative integer, the items of the array can be obtained from the end of the array.


let array = [1, 2, 3, 4, 5, 6, 7] 
const firstArrayVal = array.slice(0, 1) 
> Result: [1] 

const lastArrayVal = array.slice(-1) 
> Result: [7] 

console.log(array.slice(1)) 
> Result: (6) [2, 3, 4, 5, 6, 7] 

console.log(array.slice(array.length)) 
> Result: []

In addition to using array. slice (-1) to get the last entry of the array, as shown in the above example, you can also get the last entry of the array in the following ways:


console.log(array.slice(array.length - 1)) 
> Result: [7]

Get the maximum and minimum values from the array

You can use Math. max and Math. min to fetch the maximum and minimum values in the array:


const array = [1, 1, 2, 3, 5, 5, 1] 
Array.from(new Set(array)) 

> Result:(4) [1, 2, 3, 5]
0

You can also use the... operator of ES6 to do this:


const array = [1, 1, 2, 3, 5, 5, 1] 
Array.from(new Set(array)) 

> Result:(4) [1, 2, 3, 5]
1

Above is JavaScript 1 Tips to share the details, more information about JavaScript Tips please pay attention to other related articles on this site!


Related articles: