JS//日期 Math 数组API 对象API

在这里插入图片描述

JS——日期 Math 数组API 对象API

1 知识点

1.1 日期

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 数组API

1)

数组的“增删查改”:

  • 增:(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)// 返回布尔值

(原数组上改变9:pop、shift、push、unshift、reverse、splice、sort、copyWithin、fill)
(不改变原数组:join、toLocaleString、toString、slice、concat、indexOf、lastIndexOf、includes)

2)

数组的遍历方法:
(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()

(原数组上改变:)
(生成新数组:map)

1.4 对象API

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

i:key
obj[i]:value
  • 增:obj.key = value / obj[‘property’] = value
  • 改:obj.key = newValue
  • 删:delete obj.key
  • 查: obj.hasOwnProperty(‘property’) //返回true、false
    for … in …
    Object.keys(obj) //返回obj对象中key值组成的数组
    Object.values(obj)
    Object.entries(obj)

方括号符号让我们能用一个变量作为属性名来访问对象的属性
var aa = “name”
obj[aa] = “美美”

2 问答

题目:
*获取 2020-10-21 格式的日期
*获取随机数,要求是长度一定的字符串格式
*写一个能遍历对象和数组的通用 forEach 函数

2.1 获取 2020-10-21 格式的日期

在这里插入图片描述

2.2 获取随机数,要求是长度一定的字符串格式

在这里插入图片描述

2.3 写一个能遍历对象和数组的通用 forEach 函数

在这里插入图片描述

    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)
    })

猜你喜欢

转载自blog.csdn.net/weixin_37877794/article/details/114198906
今日推荐