JS//Date Math Array API Object API

Insert picture description here

JS-Date Math Array API Object API

1 knowledge points

1.1 Date

Date.now( )  //获取当前时间毫秒数
var dt = new Date( )
dt.getTime( )  //获取毫秒数
dt.getYear  //年(2位)
dt.getFullYear( )  //年(4位)
dt.getMonth( )  //月(0-11)
dt.getDate( )  //日(1-31)
dt.getDay( )  //星期*(0-6)
dt.getHours( )  //时(0-23)
dt.getMinutes( )  //分(0-59)
dt.getSeconds( )  //秒(0-59)

1.2 Math

获取随机数 Math.random( )

1.3 Array API

1)

"Add, delete, check and modify" the array:

  • 增:(push、unshift)(join、concat)
  • 删:(pop、shift)
  • 查:(indexOf、lastIndexOf、includes)(slice)
  • 改:(reverse、splice、sort、copyWithin、fill)(toLocaleString、toString)
array.splice(index,howmany,item1......itemX)
array.sort(function(a,b){
    
     return b-a; }))
array.copyWithin(target,start=0,end=this.length)
array.fill(value,start=0,end=this.length)
array.indexOf(searchElement,fromIndex)// 返回数字
array.includes(searchElement,fromIndex)// 返回布尔值

(Change 9 on the original array: pop, shift, push, unshift, reverse, splice, sort, copyWithin, fill)
(Do not change the original array: join, toLocaleString, toString, slice, concat, indexOf, lastIndexOf, includes)

2)

Array traversal method:
(forEach, every, some, fliter, map, reduce, reduceRight,
find, findIndex, keys, values, entries)

* array.forEach(function(currentValue, index, arr), thisValue)  // 没有返回值
* array.every(function(currentValue, index, arr), thisValue)  // 返回true、false
* array.some(function(currentValue, index, arr), thisValue)  // 返回true、false
* let new_array = arr.filter(function(currentValue, index, arr), thisArg)  // 返回新数组
* let new_array = arr.map(function(currentValue, index, arr), thisValue) // 返回新数组
累加器,合并为一个值——
* array.reduce(function(prevValue, currentValue, currentIndex, arr), initialValue) 
根据条件找到数组成员——
* let new_array = arr.find(function(currentValue, index, arr), thisValue)
* let new_array = arr.findIndex(function(currentValue, index, arr), thisValue)
遍历键名、遍历键值、遍历键名+键值——
*array.keys()
*array.values()
*array.entries()

(Change on the original array:)
(Generate a new array: map)

1.4 Object API

for ( ** in **) {
    
     ... }

i:key
obj[i]:value
  • 增:obj.key = value / obj[‘property’] = value
  • 改:obj.key = newValue
  • 删: delete obj.key
  • Check: obj.hasOwnProperty('property') //Returns true, false
    for… in…
    Object.keys(obj) //Returns an array of key values ​​in the obj object
    Object.values(obj)
    Object.entries(obj)

The square bracket notation allows us to use a variable as a property name to access the properties of an object
var aa = "name"
obj[aa] = "美美"

2 Q&A

Topic:
*Get the date in 2020-10-21 format
*Get random numbers, the requirement is a string format with a certain length
*Write a general forEach function that can traverse objects and arrays

2.1 Get the date in 2020-10-21 format

Insert picture description here

2.2 Obtaining random numbers, requires a string format with a certain length

Insert picture description here

2.3 Write a general forEach function that can traverse objects and arrays

Insert picture description here

    function forEach(obj, fn) {
    
    
      var key
      if (obj instanceof Array) {
    
    
        obj.forEach(function(index, item) {
    
    
          fn(index, item)
        })
      } else {
    
    
        for (key in obj) {
    
    
          fn(key, obj[key])
        }
      }
    }

    var arr = [1,2,3]
    forEach(arr, function(index, item) {
    
    
      console.log(index, item)
    })

    var pc = {
    
    husband: 'songosng', wife: 'meimei'}
    forEach(pc, function(key, val) {
    
    
      console.log(key, val)
    })

Guess you like

Origin blog.csdn.net/weixin_37877794/article/details/114198906