美文网首页
字符串与JSON

字符串与JSON

作者: S级食材咩咩羊 | 来源:发表于2017-03-23 22:31 被阅读0次

    1.使用数组拼接出如下字符串

    var prod = {
        name: '女装',
        styles: ['短款', '冬季', '春装']
    };
    function getTpl(data)
    {
        var arr = [];
        arr.push("<dl class=\"product\">"+"\n");
        arr.push("\t"+"<dt>"+ data.name + "</dt>" + "\n");
        for (var key in data.styles)
        {
            arr.push("\t"+"<dd>"+ data.styles[key] + "</dd>" + "\n");
        }
        arr.push("</dl>");
        return arr.join("");
    };
    var result = getTpl(prod);
    console.log(result); 
    

    2.写出两种以上声明多行字符串的方法.

    • 每一行的尾部使用“/”
    • "+"运算符拼接
    • 注释法
    (function () { /*
    line 1
    line 2
    line 3
    */}).toString().split('\n').slice(1,-1).join('\n')
    // "line 1 line 2 line 3"
    

    3.补全如下代码,让输出结果为字符串: hello\饥人谷

    var str = "hello\\\\饥人谷"//补全代码
    console.log(str) //result为下面的字符串
    

    4.以下代码输出什么?为什么

    var str = 'jirengu\nruoyu'
    console.log(str.length) 
    //13,\n为一个转义字符
    

    5.写一个函数,判断一个字符串是回文字符串,如 abcdcba是回文字符串, abcdcbb不是

    function isRollback (data) 
    {
        var str;
        str = data.split('').reverse().join(''); 
        if (str === data) 
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    var result = isRollback('abcddcba');
    console.log(result);
    

    6.写一个函数,统计字符串里出现出现频率最多的字符

    function maxChar (data) 
    {
        var dict = {};
        for (var i = 0; i < data.length; i++)
        {
            if (dict[data[i]]) 
            {
                ++dict[data[i]];
            }
            else 
            {
                dict[data[i]] = 1;  
            }
        }
    
        var count = 0;
        var maxValue = 0;
        for (Key in dict)
        {
            if (dict[Key] > count) 
            {
                maxValue = Key;
                count = dict[Key];
            }
        }
        console.log(dict);
        return maxValue;
    }
    var result = maxChar('hello');
    console.log(result);
    

    7.写一个camelize函数,把my-short-string形式的字符串转化成myShortString形式的字符串,如

    function camelize (str) 
    {
        var temp = str.split('-');
        var poi = '';
        var result = '';
        for (var i = 0; i < temp.length; i++)
        {
            if (0 != i ) 
            {
                var yamato = temp[i].split('');
                yamato[0] = yamato[0].toUpperCase();
                poi = yamato.join('');
            }
            else 
            {
                poi = temp[i];
            }
            result = result + poi;
        }
        return result;
    }
    
    console.log(camelize("background-color") == 'backgroundColor'); 
    console.log(camelize("list-style-image") == 'listStyleImage'); 
    

    8.写一个 ucFirst函数,返回第一个字母为大写的字符 (***)

    function ucFirst (str) 
    {
        var temp = str.split('');
        temp[0] = temp[0].toUpperCase();
        var result = temp.join('');
    
        return result;
    }
    
    console.log(ucFirst("hunger") == "Hunger"); 
    

    9.写一个函数truncate(str, maxlength), 如果str的长度大于maxlength,会把str截断到maxlength长,并加上...,如

    function truncate (str, len) 
    {
        var result = str.substr(0,len);
        if (result.length === len) 
        {
            result = result + '...';
        }
    
        return result;
    }
    
    console.log(truncate("hello, this is hunger valley,", 10) == "hello, thi..."); 
    console.log(truncate("hello world", 20) == "hello world"); 
    

    10.什么是 json?什么是 json 对象?什么是 json 对象字面量?什么是 JSON内置对象?11、如何把JSON 格式的字符串转换为对象?如何把对象转换为 JSON 格式的字符串?

    • json:JavaScript object Notation,是一种轻量级的数据交换格式。
    • json 对象:json格式的一个无序的键值对集合
    • json 对象字面量:json 对象里面的键值对
    • JSON内置对象:经过JSON.parser()转换的值

    11.如何把JSON 格式的字符串转换为对象?如何把对象转换为 JSON 格式的字符串?

    • parser:把字符串转换成JSON对象
    • stringify:把JSON对象转换为字符串

    相关文章

      网友评论

          本文标题:字符串与JSON

          本文链接:https://www.haomeiwen.com/subject/zebtottx.html