JavaScript对JSON数组操作。数组添加(push)以及移除(splitce)

这里写一个小栗子提供给大家学习和纪录。 
js声明数组 以及向数组中添加as移除json数据

JavaScript声明JSON数组的方法:

//部分条件,在数据渲上数据要求是数组格式而非json数组格式,取arrayJson.dataList
var arrayJson = {'dataList':[]};
  • 1
  • 2

向数组中添加json串

arrayJson.dataList.push({'name':'dataName'});
  • 1

移除所有数组中数据

arrayJson.dataList.splice(0,arrayJson.dataList.length);
  • 1

一下举栗声明json数组串较为复杂并且灵活的格式。

[
    {
        "dateMonth":"05",
        "hopeMoney":"12000",
        "data":[
            {
                "brickId":"1",
                "brickYear":"2017-05-15",
                "brickWeek":"星期二",
                "brckInfo":[
                    {
                        "bringMoneyOrExpenditure":"5000",
                        "brickClass":"工资",
                        "brickIcon":"ion-ios-star-outline"
                    },
                    {
                        "bringMoneyOrExpenditure":"-2000",
                        "brickClass":"其他",
                        "brickIcon":"ion-ios-star-outline"
                    }
                ]
            },
            {
                "brickId":"1",
                "brickYear":"2017-05-02",
                "brickWeek":"星期六",
                "brckInfo":[
                    {
                        "bringMoneyOrExpenditure":"10000",
                        "brickClass":"工资",
                        "brickIcon":"ion-ios-star-outline"
                    }
                ]
            }
        ]
    },
    {
        "dateMonth":"04",
        "hopeMoney":"12000",
        "data":[
            {
                "brickId":"1",
                "brickYear":"2017-04-15",
                "brickWeek":"星期一",
                "brckInfo":[
                    {
                        "bringMoneyOrExpenditure":"5000",
                        "brickClass":"工资",
                        "brickIcon":"ion-ios-star-outline"
                    }
                ]
            }
        ]
    }
]

你可能感兴趣的:(JavaScript对JSON数组操作。数组添加(push)以及移除(splitce))