Methods take you in learning new ES5

1. ES5 added in a number of ways, can easily manipulate arrays or strings, these methods include the following aspects

  • Array Methods
  • String Methods
  • Object Methods

    2. Array method

    Iterative traversal methods: forEach (), map () , filter (), some (), every ()
    Analyzing method: isArray ()

2.1 forEach similar with each use jQuery. The syntax is:

array.forEach(function(currentValue, index, arr))
  • currentValue: array value of the current item
  • index: Array index of the current item
  • arr: array object itself
var arr = [1,2,3]
arr.forEach(function(value, index, array){
    console.log('每个数组元素'+ value)
  console.log('每个数组元素的索引值'+ index)
  console.log('数组本身'+ array)
})

Operating results below
image.png

2.2 map()

array.map(function(currentValue, index, arr))

map () method creates a new array, the result is returned after the results of each element in the array to call a function provided.
Note: map () method is the direct returns a new array

  • currentValue: array value of the current item
  • index: Array index of the current item
  • arr: array object itself
var array1 = [1, 4, 9, 16];
var map1 = array1.map(function(value, index, arr) {
return value * 2
});
console.log(map1) //[2,8,18,32]

2.3 filter()

array.filter(function(currentValue, index, arr))

filter () method for screening to create a new array of new elements in the array is specified by examining all of the elements in the array qualified, mainly used for screening arrays.
filter () method is returned directly to a new array

  • currentValue: array value of the current item
  • index: Array index of the current item
  • arr: array object itself
var arr = [12,66,88]
// 选出大于20的
var newarr = arr.filter(function(value, index){
  return value >=20
})
可以简写成var newarr = arr.filter( //[66, 88])
console.log(newArr) //[66, 88]

FIG follows preview
image.png

2.4 some()

array.some(function(currentValue, index, arr))

some () method for detecting elements in the array satisfies the specified conditions, popular point find whether there is the condition elements in the array
Note: some () method returns a Boolean value, to find if this element, it returns true, if find less than false returns
if found to meet the conditions of the first elements of the loop ends, not to continue to find

  • currentValue: array value of the current item
  • index: Array index of the current item
  • arr: array object itself
var arr=[10,30,4]
var b = arr.some(function(value){
    return value > 20
})
console.log(b) //true

2.5 every()

array.every(function(currentValue, index, arr))

every () method to test all the elements in an array can test whether a given function by. It returns a Boolean value.
Note: every () method must meet the criteria to each element returns true, if a non-compliance, false is returned.
If you receive an empty array, this method returns true in all cases.

  • currentValue: array value of the current item
  • index: Array index of the current item
  • arr: array object itself

    2.6 isArray()

Array.isArray(obj)


Array.isArray whether the value of () is used to determine a transfer Array

  • obj is a value to be detected. If it returns true then the array, false otherwise

the difference:

  1. filter is to find the elements to meet the conditions, returns an array, but also to meet the conditions of all the elements return back
  2. Find some is to meet the conditions of the element exists, it returns a Boolean value, if finds the first element satisfies the conditions on the termination of the cycle. If the only elements in the array query, use some method is more appropriate because it finds that element, not making the cycle more efficient
  3. every element is to find all of the eligibility, it returns a Boolean value
  4. map is to create a new array, the result is returned after the results of each element in the array to call a function provided

    3. The method of string

    trim () method removes the whitespace characters from both ends of a string

str.trim()

trim () method does not affect the original string itself, it returns a new string.

4. Object Methods

Object.defineProperty () defines object attributes to add or modify the properties of the original

Object.defineProperty(obj, prop, descriptor)
  • obj: Required. target
  • prop: Required. You need to define or modify the attribute name
  • descriptor: Required. Target property owned properties

Object.defineProperty () Description third parameter descriptor: written as an Object {}

  • value: Sets the value of a property, the default is undefined
  • writable: whether the value can be overridden. true | false Default is false
  • enumerable: whether the target property can be enumerated. true | false Default is false
  • configurable: whether the target attribute can be deleted or whether you can modify the characteristics of true again | false Default is false
var obj = {
id: 1,
pname: '小米',
price: 1999
}
//以前的对象添加和修改属性的方式
obj.num = 1000
obj.pirce = 99
console.log(obj)

//Object.deefineProperty()定义新属性或者修改原有的属性
Object.defineProperty(obj, 'num',{
    value: 1000,
})
Object.defineProperty(obj, 'pricee',{
    value: 9.9,
})
Object.defineProperty(obj, 'id',{
  //如果值为false,则不允许修改
    writable: false,
})
Object.defineProperty(obj, 'address',{
    value: '中国郑州市',
  //如果值为false,则不允许遍历,枚举
  enumerable: false
})
console.log(obj)
console.log(Object.keys(obj))

to sum up

This article is mainly to share some of the ways the new ES5, there is an array of methods, string methods, object methods, and so on, the main method of sharing usage brother, characteristics and so on. If you want to know more, scan the QR code:
qrcode_for_gh_4d3763fa9780_258 (1).jpg

Guess you like

Origin www.cnblogs.com/lfcss/p/12391899.html