zoukankan      html  css  js  c++  java
  • 总结下常用js中的小语法和技巧

    1,数组对象遍历

    对一个级数对象进行遍历,取出每个值 

        var arr={
            "result":[
                {"time":"2018-10-24 12:12:12"},
                {"time":"2018-10-25 12:12:12"},
                {"time":"2018-10-26 12:12:12"},
                {"time":"2018-10-27 12:12:12"},
                {"time":"2018-10-28 12:12:12"},
                {"time":"2018-10-29 12:12:12"},
            ]
        }
    
        for(let ind in arr.result){  //对时间格式进行处理,删除年份和秒
            let index = ind;    //对象中的每一项的键名
            let value = arr.result[index].time;   //对象中的每一项的值
            console.log("valueTime:",value);
        }
        //输出结果为:  valueTime: 2018-10-24 12:12:12
        // valueTime: 2018-10-25 12:12:12
        // valueTime: 2018-10-26 12:12:12
        // valueTime: 2018-10-27 12:12:12
        // valueTime: 2018-10-28 12:12:12
        // valueTime: 2018-10-29 12:12:12

    2,substring()方法截取字符串指定前几位或后几位

    js方法substring()常用于处理时间或文本,也可以用正则匹配,但该方法更准确快速

        var str = "2018-10-24 12:12:12";
        var prevStr= str.substring(5);
        var nextStr = str.substring(0,str.length-3)
    
        console.log("prevStr:",prevStr)
        console.log("nextStr:",nextStr)
        //输出结果为:prevStr: 10-24 12:12:12
        // nextStr: 2018-10-24 12:12
  • 相关阅读:
    355. Design Twitter
    54. Spiral Matrix
    143. Reorder List
    324. Wiggle Sort II
    365. Water and Jug Problem
    洛谷 P3527 [POI2011]MET-Meteors 解题报告
    洛谷 P4592 [TJOI2018]异或 解题报告
    单调序列 解题报告
    洛谷 P4735 最大异或和 解题报告
    洛谷 P1527 [国家集训队]矩阵乘法 解题报告
  • 原文地址:https://www.cnblogs.com/zhixi/p/9849003.html
Copyright © 2011-2022 走看看