Home  >  Article  >  Web Front-end  >  A brief analysis of JS dates, Math, arrays and objects

A brief analysis of JS dates, Math, arrays and objects

小云云
小云云Original
2018-03-05 11:02:101215browse

This article mainly shares with you a brief analysis of JS dates, Math, arrays and objects, hoping to help everyone.

Related questions

  • Get the date in 2017-06-10 format

function formatDate(dt){    if(!dt){
        dt = new Date();
    }    var year = dt.getFullYear();var month = dt.getMonth()+1;var date = dt.getDate();if(month<10){    //强制类型转换
    month = &#39;0&#39;+month;
}if(date<10){    date = &#39;0&#39;+date;
}return year +  &#39;-&#39;+month+&#39;-&#39;+date;
}var dt = new Date();var formatDate = formatDate(dt);
console.log(format);
  • Get random Number, the requirement is a string format of consistent length (the role of random numbers in front-end development)

var random =Math.random();random= random + &#39;0000000000&#39;;//保证下一句不会报错random = random.slice(0,9);
console.log(random);
  • Write a general forEach that can traverse objects and arrays Function (available in jquery)

//想想对象中的forEach与数组中的for in怎么用比较好//这里是forEach函数function forEach(obj,fn){
    var key;    if(obj instanceof Array){
        obj.forEach(function(item,index){
            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 obj = {x:100,y:200};forEach(obj,function(key,value){
    console.log(key,value);
})

Knowledge point

  • Date

Date.nowTime(); //获取当前时间毫秒数(这个数是从1970开始算起到现在走了多少毫秒数)var myDate = new Date(); //new一个时间对象myDate.getTime(); //获取毫秒数myDate.getFullYear(); //年(4位数),还有一种getYear()的方法,有兼容性的问题,与getFullYear()相差1900myDate.getMonth(); //月(0-11),实际应用是应该注意它的返回值myDate.getDate();//日(1-31)myDate.getDay();//星期(0-6),实际应用是应该注意它的返回值myDate.getHours();//时(0-23)myDate.getMinutes();//分(0-59)myDate.getSeconds();//秒(0-59)
  • Math
    1. Get a random number Math.random();

will return a decimal number greater than 0 and less than 1, eg: 0.14249200181060218, which is generally used to clear cache For example: when the page is opened, due to the existence of cache, the newly updated data sometimes cannot be refreshed on the page, because if the requested address is the same, the browser will not connect to the server. At this time, random numbers can be used to process it, which is equivalent to loading a new page.

在URL 参数后加上 "?r=" + Math.random();//加随机数在 URL 参数后加上 "?timestamp=" + new Date().getTime(); //也可以加时间戳

2. For more methods and random number extensions, see my previous blog;

  • Array API
    1. forEach traverses all elements

var arr = [1,2,3];
arr.forEach(function(item,index){//item为元素的值eg:1,2,3,index位为元素的位置,从0开始的
    //遍历数组中所有元素
    console.log(index,item);    //注意参数顺序});

2 . every determines whether all elements meet the conditions

var arr = [1,2,3];var result = arr.every(funciton(item,index){// 用来判断所有的数组元素,都满足一个条件
    if(item < 4){        return ture;
    }
});
console.log(result);//true

3 . some determines whether at least one element meets the conditions

var arr = [1,2,3];var result = arr.every(funciton(item,index){// 用来判断所有的数组元素,都满足一个条件
    if(item < 2){        return ture;
    }
});
console.log(result);//true

4 . sort Sort (from small to large) Sort toward larger)

var arr = [1,4,3,2,5];var arr2 = arr.sort(function(a,b){
    //从小到大
    return a-b;    //从大到小
    //return b-a;});
console.log(arr2);//[1,2,3,4,5]

5. map reassembles the elements to generate a new array (assembly of elements into another type of elements according to a rule to generate a new array)

var arr = [1,2,3,4];var arr2 = arr.map(function(item,index){    //将元素重新组装并返回    return &#39;<b>'+item+'</b>';
});console.log(arr2);  //["<b>1</b>", "<b>2</b>", "<b>3</b>", "<b>4</b>"]

6. Filter elements that meet the conditions

var arr = [1,2,3];var arr2 = arr.filter(function(item,index){
    //通过一个条件过滤数组
    if(item>=2){        return true;
    }
});
console.log(arr2); //2,3
  • Object API

var obj ={x:100,y:200,z:300};var key;for(key in obj){//key这里取得是obj的属性名
    if(obj.hasOwnProperty(key)){//判断他是这个obj的属性而不是原型的属性
        console.log(key,obj[key]);
    }
}

Related recommendations:
JS date object simple operation (Get the current year, week, and time)

JS date addition and subtraction function sorting

js date related function summary sharing_javascript skills

The above is the detailed content of A brief analysis of JS dates, Math, arrays and objects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn