zoukankan      html  css  js  c++  java
  • 一些Js操作

    一、after()和before()方法的区别

        after()——其方法是将方法里面的参数添加到jquery对象后面去;
        如:A.after(B)的意思是将B放到A后面去;
        before()——其方法是将方法里面的参数添加到jquery对象前面去。
        如:A.before(B)的意思是将A放到B前面去; 

    二、字符串去空格和去换行

    //去掉空格 str = str.replace(/ +/g,"");

    //去掉回车换行 str = str.replace(/[ ]/g,"");

    三、json字符串和json对象的转换

    $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 

    JSON.parse(jsonstr); //可以将json字符串转换成json对象 

    JSON.stringify(jsonobj); //可以将json对象转换成json对符串 

    eval('(' + jsonstr + ')'); //可以将json字符串转换成json对象,注意需要在json字符外包裹一对小括号 

    四、字符串中首字母大写

    String.prototype.toJadenCase = function () {
    return this.split(/s+/).map(function(word){
    return word.charAt(0).toUpperCase()+word.slice(1);
    }).join(" ")
    };
    var str = "How can mirrors be real if our eyes aren't real";
    Test.assertEquals(str.toJadenCase(), "How Can Mirrors Be Real If Our Eyes Aren't Real");

     五、清除重复的信息

    // 清除重复的产品
    let newarr = []
    this.products.map((item,index)=>{
        let temp = JSON.stringify(item)
        if(JSON.stringify(newarr).indexOf(temp)<0){
           newarr.push(item)
        }
    })

    (1)定义一个空数组;(2)遍历需要处理的数组对象products;(3)获取到数组对象中的每一个值,和push进新数组的内容做比较,如果有重复内容,就不push。

    知识点:(1)str.indexOf(item):判断str字符串中是否存在item内容,如果存在,返回出现的位置,否则,返回-1.

        (2)JSON.stringify(item):将一个JavaScript值(对象或者数组)转换为一个 JSON字符串。

  • 相关阅读:
    [fw]error: aggregate value used where an integer was expected
    [fw]awk求和
    [fw]谈EXPORT_SYMBOL使用
    [fw]用Kprobes调试(debug)内核
    [FW]使用kprobes查看内核内部信息
    linux缺頁異常處理--內核空間[v3.10]
    用C语言给指定的内存地址赋值(通过指针)
    [fw]Linux 的 time 指令
    how to prevent lowmemorykiller from killing processes
    Android成长日记-Noification实现状态栏通知
  • 原文地址:https://www.cnblogs.com/zjingjing/p/8691304.html
Copyright © 2011-2022 走看看