ES6-new method

1. includes

Determine whether a string contains certain characters or whether an array contains a certain element

1. Basic usage

console.log('abc'.includes('a'));//true
console.log('abc'.includes('d'));//false
console.log([1,2,3].includes(1));//true
console.log([1,2,3].includes('1'));//false

2. The second parameter
indicates the starting position of the search. The default is 0.

console.log('abc'.includes('a',1));//false

2. padStart() and padEnd()

Complete string length

1. Basic usage

console.log('x'.padStart(5,'ab'));//ababx
console.log('x'.padEnd(5,'ab'));//xabab

2. Things to note

  • The length of the original string is equal to or greater than the maximum length. The original string will not be reduced. String completion will not take effect and the original string will be returned.
console.log('xxx'.padEnd(2,'ab'));//xxx
  • The sum of the lengths of the string used for completion and the original string exceeds the maximum length. The completion string that exceeds the number of digits is truncated and the original string remains unchanged.
console.log('abc'.padStart(10, '0123456789'));//0123456abc
  • If the second parameter is omitted, spaces will be used to complete the length by default.

3. trimStart() and trimEnd()

Clear the leading or trailing spaces of the string. The spaces in the middle will not be cleared.

4. Array.from() of array

Convert other data types into arrays.
All traversable
arrays, strings, Sets, Maps, RiodeLists, and arguments.
Any object with a length attribute.

1. Basic usage

console.log(Array.from('str'));//[ 's', 't', 'r' ]
const obj = {
    
    
	'0':'a',
	'1':'b',
	name: 'Alex',
	length: 3
};
console.log(Array.from(obj));//[ 'a', 'b', undefined ]

2. The second parameter
is similar to the map method of an array. It is used to process each element and put the processed value into the returned array.

console.log(
	[1,2].map(value => {
    
    return value * 2})
);//[ 2, 4 ]
console.log(Array.from('12', value => value * 2));//[ 2, 4 ]

5. find() and findIndex()

find(): Find one that meets the conditions and return immediately.
findIndex(): Find one that meets the conditions and return an index immediately.

1. Basic usage

console.log(
	[1,5,10,15].find((value) =>{
    
    
	return value > 9;
	})
);//10

console.log(
	[1,5,10,15].findIndex((value) =>{
    
    
	return value > 9;
	})
);//2

六、Object.assign()

Merge objects.
The later ones with the same attributes will overwrite the previous ones.
Multiple objects can be merged.

const apple = {
    
    
	color: '红色',
	shape: '圆形',
	taste: '甜'
};
const pen = {
    
    
	color: '黑色',
	shape: '圆柱形',
	use: '写字'
};

console.log(Object.assign(apple,pen));
//{ color: '黑色', shape: '圆柱形', taste: '甜', use: '写字' }
console.log(apple)//{ color: '黑色', shape: '圆柱形', taste: '甜', use: '写字' }
console.log(Object.assign(apple,pen)===apple)//true

七、Object.keys). Object.values()和Object.entries()

Get the key value of the object

const person = {
    
    
	name: 'Alex',
	age: 18
};
console.log(Object.keys(person));//[ 'name', 'age' ]
console.log(Object.values(person));//[ 'Alex', 18 ]
console.log(Object.entries(person));//[ [ 'name', 'Alex' ], [ 'age', 18 ] ]

Guess you like

Origin blog.csdn.net/qq_42042158/article/details/126008039