柳暗花明笔记

    javascript 数组循环及按指定字符进行拆解成数组

    所属分类:[ php&web ] 猫哲学家 发表于:2017-10-19 19:34 浏览:120

    var dat = [];
    var aa = "1-1,2,456|2-564|6-3566";
    var it = aa.split('|');
    it.forEach(function(e) {
    var cc = e.split('-');
    dat[cc[0]] = cc[1];
    });
    dat[6] = 1113;
    var str = '';
    for (var k in dat){
    if (typeof dat[k] !== 'function') {
    str += k + "-"+dat[k]+"|";
    console.log("Key is " + k + ", value is " + dat[k]);
    }
    }
    str = str.slice(0, -1);
    console.log(str);

    =======进阶版1============

    var setPlatformCategoryFormValue = function (platformid, siteid, categoryid) {
    if (platformid == '' || siteid == '' || categoryid == '') {
    return false;
    }
    var s = $("input[name='platformCateorySetting']");
    var a = [];
    var b = s.val();
    var c = b.split('|');
    c.forEach(function(e) {
    var d = e.split('-');
    a[d[0]] = [];
    a[d[0]].push({
    siteid: d[1],
    categoryid: d[2],
    });
    });
    a[platformid][0].siteid = siteid;
    a[platformid][0].categoryid = categoryid;
    var f = '';
    for (var k in a) {
    if (typeof a[k] !== 'function') {
    f += k + '-' + a[k][0].siteid + '-' + a[k][0].categoryid + '|';
    }
    }
    f = f.slice(0, -1);
    s.val(f);
    }

    ======进阶版2=======

    var setPlatformCategoryFormValue = function (platformid, siteid, categoryid) {
    if (platformid == '' || siteid == '' || categoryid == '') {
    return false;
    }
    var s = $("input[name='platformCateorySetting']");
    var a = [];
    var b = s.val();
    var c = b.split('|');
    c.forEach(function(e) {
    var d = e.split('-');
    a[d[0]] = {
    siteid: d[1],
    categoryid: d[2],
    };
    });
    a[platformid].siteid = siteid;
    a[platformid].categoryid = categoryid;
    var f = '';
    for (var k in a) {
    if (typeof a[k] !== 'function') {
    f += k + '-' + a[k].siteid + '-' + a[k].categoryid + '|';
    }
    }
    f = f.slice(0, -1);
    s.val(f);
    }

    标签: javascript 
    上一编文章 mysql 查看数据库大小 下一编文章 Got error 28 from storage engine
    最近文章
    发表评论