鍍金池/ 問答/HTML/ 前端拼接json字符串

前端拼接json字符串

1.后端想要的json字符串如下

{
    "key":"q",
    "name":"請假流程",
    "productID":"1",
    "nodeList":[
        {
            "id":"241",
            "node_name":"新步驟",
            "creation_time":"",
            "creation_time_stamp":"",
            "role_id":"person",
            "next_role_id":"manager",
            "last_step_id":"",
            "next_step_id":"242",
            "admin_id":"",
            "remark":"",
            "workflow_id":"h",
            "style":"width:121px;height:41px;line-height:41px;color:#0e76a8;left:532px;top:186px;",
            "product_id":"1",
            "sort":""
        },
        {
            "id":"242",
            "node_name":"新步驟",
            "creation_time":"",
            "creation_time_stamp":"",
            "role_id":"person",
            "next_role_id":"manager",
            "last_step_id":"241",
            "next_step_id":"243",
            "admin_id":"",
            "remark":"",
            "workflow_id":"h",
            "style":"width: 121px; height: 41px; line-height: 41px; color: rgb(14, 118, 168); left: 1030px; top: 374px;",
            "product_id":"1",
            "sort":""
        },
        {
            "id":"243",
            "node_name":"新步驟",
            "creation_time":"",
            "creation_time_stamp":"",
            "role_id":"person",
            "next_role_id":"manager",
            "last_step_id":"242",
            "next_step_id":"",
            "admin_id":"",
            "remark":"",
            "workflow_id":"h",
            "style":"width: 121px; height: 41px; line-height: 41px; color: rgb(14, 118, 168); left: 608px; top: 392px;",
            "product_id":"1",
            "sort":""
        }
    ]
}

2.我開始拼接字符串

var aProcessData= {
                key: "q",
                name:"請假流程",
                productID:"1",
                nodeList:[

                ]
            };
var nodeListObj={
                        id:id,
                        node_name:node_name,
                        creation_time:"",
                        creation_time_stamp:"",
                        role_id:"person",
                        next_role_id:"manager",
                        last_step_id:last_step_id,
                        next_step_id:next_step_id,
                        admin_id:"",
                        remark:"",
                        workflow_id:"h",
                        style:style,
                        product_id:"1",
                        sort:""
                    };

遍歷數(shù)組,把nodeListObj添加進aProcessData.nodeList
aProcessData.nodeList.push(nodeListObj)
3.感覺這樣寫死是不對的啊,如果要加一些判斷之類的,比如當(dāng)next_step_id為空時,不要這個字段
那我這樣子寫可以嗎

var nodeListObj={};
nodeListObj.id=id;
nodeListObj.node_name=node_name;
nodeListObj.creation_time="";
nodeListObj.creation_time_stamp="";
nodeListObj.role_id="person";
if(last_step_id !="") nodeListObj.last_step_id=last_step_id;
if(next_step_id !="") nodeListObj.next_step_id=next_step_id;

4.有沒有更優(yōu)雅的方式去拼接json字符串

回答
編輯回答
別逞強

1、刪除字段可以使用delete

var obj={a:1,b:2};
delete obj.a;
console.log(obj);
// {b: 2}

2、一個簡單的方式

var yourObj=JSON.parse(`{
    "key":"q",
    "name":"請假流程",
    "productID":"1",
    "nodeList":[
        {
            "id":"241",
            "node_name":"新步驟",
            "creation_time":"",
            "creation_time_stamp":"",
            "role_id":"person",
            "next_role_id":"manager",
            "last_step_id":"",
            "next_step_id":"242",
            "admin_id":"",
            "remark":"",
            "workflow_id":"h",
            "style":"width:121px;height:41px;line-height:41px;color:#0e76a8;left:532px;top:186px;",
            "product_id":"1",
            "sort":""
        },
        {
            "id":"242",
            "node_name":"新步驟",
            "creation_time":"",
            "creation_time_stamp":"",
            "role_id":"person",
            "next_role_id":"manager",
            "last_step_id":"241",
            "next_step_id":"243",
            "admin_id":"",
            "remark":"",
            "workflow_id":"h",
            "style":"width: 121px; height: 41px; line-height: 41px; color: rgb(14, 118, 168); left: 1030px; top: 374px;",
            "product_id":"1",
            "sort":""
        },
        {
            "id":"243",
            "node_name":"新步驟",
            "creation_time":"",
            "creation_time_stamp":"",
            "role_id":"person",
            "next_role_id":"manager",
            "last_step_id":"242",
            "next_step_id":"",
            "admin_id":"",
            "remark":"",
            "workflow_id":"h",
            "style":"width: 121px; height: 41px; line-height: 41px; color: rgb(14, 118, 168); left: 608px; top: 392px;",
            "product_id":"1",
            "sort":""
        }
    ]
}`)

3、再根據(jù)條件刪除,修改屬性,就好了。

2018年7月14日 01:17
編輯回答
貓館

沒有了,對象不就是這樣的嗎?不要字段是不可能,肯定是為空''或者null就看你和后臺商議了

2018年8月12日 14:15