First of all: I do not cite the most basic examples, you have said rotten, such as this and this, I only tell the most practical and commonly used examples, you copy, you can use the example
This article refers to the business done, the actual advanced narration, what you can use them, how should use them in the project? This is for you to look at.
The array object determines whether the specified value exists
const list = [
{ name: 'Head navigation'.backward: false },
{ name: '轮播'.backward: true },
{ name: 'footer'.backward: false},];const someBackward = list.some(item= > item.backward);
// someBackward: true
const everyNewest = list.every(item= >! item.backward);// everyNewest: false
Copy the code
- Both are used to make array conditions, and both return a Boolean value
- Both can be interrupted
- Some returns true if an element satisfies a condition, and the loop breaks; Return false if all elements do not meet the condition.
- Every is the opposite of some. If the beneficial element does not satisfy the condition, false is returned and the loop breaks. Return true if all elements satisfy the condition.
Array to heavy
This is a native JS function but very concise. Set takes any iterable object, such as a Set [1,2,3,3], and removes duplicates
let a=[1.1.1.2.3.4.5.5.5,]
[...new Set(a)]
//[1, 2, 3, 4, 5]
Copy the code
Deduplicate the array object
var obj = {};
arr = arr.reduce(function(item, next) {
obj[next.key] ? ' ' : obj[next.key] = true && item.push(next);
returnitem; } []);console.log(arr);
Copy the code
An array of segmentation
function Chunk(arr = [], size = 1) {
return arr.length ? arr.reduce((t, v) = > (t[t.length - 1].length === size ? t.push([v]) : t[t.length - 1].push(v), t), [[]]) : [];
}
const arr = [1.2.3.4.5];
Chunk(arr, 2); // [[1, 2], [3, 4], [5]]
Copy the code
Maximize or minimize an array
function Max(arr = []) {
return arr.reduce((t, v) = > t > v ? t : v);
}
function Min(arr = []) {
return arr.reduce((t, v) = > t < v ? t : v);
}
const arr = [12.45.21.65.38.76.108.43];
Max(arr); / / 108
Min(arr); / / 12
Copy the code
Expand multidimensional array
flat()
flatMap()
Copy the code
- The flat() method can group multidimensional numbers into a one-dimensional array
- Note that if there are empty values in the supplied array, they are discarded:
- Flat () also takes an optional argument that specifies the number of levels at which the nested array should be flattened. If no argument is provided, the default value 1 will be used: if the Infinity parameter is entered, the array will be flat regardless of the number of dimensions
- The flatMap() method combines map() and flat() into one method. It first creates a new array using the return value of the provided function, and then concatenates all the subarray elements of that array. An example: \
Traversal object or array object
The object.entries () function returns an array of key-value pairs for the enumerable properties of a given Object itself. This is really useful. We had a lot of trouble going through keys and values.
for(let [key,value] of Object.entries(obj1)){
console.log(`key: ${key} value:${value}`)}// Iterate over the object
//key:a value:1
//key:b value:2
//key:c value:3
// Iterate over a group of objects
//key:0 value:{a: 1, b: 2, c: 3}
//key:1 value:{a: 1, b: 2, c: 3}
Copy the code
Convert a two-dimensional array into an object
Entries turn objects into an array of key-value pairs, while object.fromentries, on the other hand, turn an array of key-value pairs into objects
const arr = [
['name'.'Lin SAN Xin'],
['age'.22],
['gender'.'male']]console.log(Object.fromEntries(arr))
// {name: 'Lin SAN xin ', age: 22, gender:' male '}
Copy the code
Converts a one-dimensional array to an object
var plants = ['Saturn'.'Earth'.'Uranus'.'Mercury'.'Venus'.'Earth'.'Mars'.'Jupiter'];
varplantsObj = {... plants }console.log(plantsObj); // {'0': 'Saturn','1': 'Earth', '2': 'Uranus','3': 'Mercury','4': 'Venus','5': 'Earth','6': 'Mars','7': 'Jupiter'}
Copy the code
Quick lookup of arrays
Use Array’s find() for a quick lookup.
const pets=[
{type:'Dog'.name:'Max'},
{type:'Cat'.name:'Karl'},
{type:'Dog'.name:'Tommy'}]let pet= pets.find(pet= >pet.type==='Dog'&&pet.name==='Tommy');
console.log(pet);
// {type: "Dog", name: "Tommy"}
Copy the code
Find the intersection of two arrays
To require the same value for both arrays, first make sure the arrays are not duplicate, and then use the filter() and includes() methods.
var plants1 = ['Saturn'.'Earth'.'Uranus'.'Mercury'.'Venus'.'Earth'.'Mars'.'Jupiter'];
var plants2 = ['Saturn'.'Earth'.'Uranus'];
var alonePlants = [...new Set(plants1)].filter(item= > plants2.includes(item));
console.log(alonePlants);
// [ 'Saturn', 'Earth', 'Uranus' ]
Copy the code
Gets the last element of the array
var arr = [1.2.3.4.5]
arr.pop() //5 This method changes the array structure and is not recommended
arr[arr.length - 1] //5 Common methods
arr.slice(-1) [0] //5 Get the last element without calculating the length of the array
Copy the code
Dynamically specifies a key name for an object’s properties
const dynamic = 'email';
let user = {
name: 'John',
[dynamic]: '[email protected]'
}
console.log(user);// outputs { name: "John", email: "[email protected]" }
Copy the code
Retrieves the specified property value from an array object and returns it as an array
const sheet=[ {type:'Dog'.name:'Max'}, {type:'Cat'.name:'Karl'}, {type:'Dog'.name:'Tommy'}]let arr =sheet.map((v,i) = >{
return v['name']}}Copy the code
Gets the query parameters for the URL
Use minimal code
/ / the first
q={};
location.search.replace(/([^?&=]+)=([^&]+)/g.(_,k,v) = >q[k]=v);
console.log(q);
{
"ie": "utf-8"."f": "8"."rsv_bp": "1"."tn": "baidu"."wd": "js%E6%97%A0%E7%A9%B7%E5%A4%A7"."oq": "%25E6%2597%25A0%25E7%25A9%25B7%25E5%25A4%25A7"."rsv_pq": "ac0117fa00057293"."rsv_t": "90bbevB0RLfLUZ4XME2%2FGHL2Uad8MPo%2Bhvu89MbGddXhQ4jpzIZRmNBfuIs"."rqlang": "cn"."rsv_enter": "1"."rsv_btype": "t"."rsv_dl": "tb"."inputT": "1127"
}
/ / the second
Object.fromEntries(new URLSearchParams(window.location.href))
Copy the code
Vue style penetrates
::v-deep .box p:{
color:red
}
// Use ::v-deep to penetrate
Copy the code
The Element UI form validates advanced knowledge
www.cnblogs.com/xyyt/p/1336…