鍍金池/ 問答/HTML/ JSON 數(shù)據(jù)拆分

JSON 數(shù)據(jù)拆分

這是在數(shù)據(jù)提交時遇到的問題。

我準備的數(shù)據(jù)結(jié)構(gòu)是這樣的:

    {
        "path": "test",
        "clients": [
            {
                "client": "1.2.2.2;1.1.1.1",
                "Access_Type": 2,
                "name": "test_01"
            },
            {
                "client": "1.2.2.4;1.1.1.4",
                "Access_Type": 1,
                "name": "test_02"
            },
            {
                "client": "1.3.3.3",
                "Access_Type": 1,
                "name": "test_03"
            }
        ]
    }

然而我在提交時需要的數(shù)據(jù)結(jié)構(gòu),卻是這樣的:

    {
        "path": "test",
        "clients": [
            {
                "client": "1.2.2.2",
                "Access_Type": 2,
                "name": "test_01"
            },
            {
                "client": "1.1.1.1",
                "Access_Type": 2,
                "name": "test_01"
            },
            {
                "client": "1.2.2.4",
                "Access_Type": 1,
                "name": "test_02"
            },
            {
                "client": "1.1.1.4",
                "Access_Type": 1,
                "name": "test_02"
            },
            {
                "client": "1.3.3.3",
                "Access_Type": 1,
                "name": "test_03"
            }
        ]
    }

注:client對應的字段,如果是多條并以“;”分隔,則做拆分處理,單條則不處理。

那么如何將數(shù)據(jù)修改為提交需要的結(jié)構(gòu)?
而對于這種JSON數(shù)據(jù)結(jié)構(gòu)的拆分,大家有什么解決方案,希望指教一下!

回答
編輯回答
話寡

上面是需要的還是下面是需要的。。。

2017年2月9日 11:10
編輯回答
兔寶寶
    var bbb = {
        "path": "test",
        "clients":[]
    };
     for(var i in aa.clients){
        var splitarr = aa.clients[i].client.split(";");
            for(var j=0;j<splitarr.length;j++){
                bbb["clients"].push({
                    "client":splitarr[j],
                    "Access_Type":aa.clients[i].Access_Type,
                    "name":aa.clients[i].name
                })
            }       
    }
    console.log(bbb);
2017年8月20日 02:11
編輯回答
清夢
let json =  {
    "path": "test",
    "clients": [
        {
            "client": "1.2.2.2;1.1.1.1",
            "Access_Type": 2,
            "name": "test_01"
        },
        {
            "client": "1.2.2.4;1.1.1.4",
            "Access_Type": 1,
            "name": "test_02"
        },
        {
            "client": "1.3.3.3",
            "Access_Type": 1,
            "name": "test_03"
        }
    ]
}
let clients = [];
json.clients.map(ele1=>{
    let {client,Access_Type,name} = ele1;
    client.split(';').map(ele2=>{
        clients.push({client:ele2,Access_Type,name})
    })
})
json['clients']= clients;
2017年9月1日 16:15