JS jsonArray操作
js對(duì)數(shù)組對(duì)象的操作以及方法的使用
如何聲明創(chuàng)建一個(gè)數(shù)組對(duì)象:
var arr = new Array();
或者
var arr = [];
如何移除所有數(shù)組中數(shù)據(jù)夜只?
arrayJson.dataList.splice(0,arr.length);
1
如何指定位置對(duì)數(shù)組對(duì)象的添加灸芳,如何將指定元素添加到數(shù)組中間位置枪孩?
/**
* 假設(shè)數(shù)組的長(zhǎng)度值為偶數(shù)寂呛,如何將指定元素添加到數(shù)組中間位置湾宙。
*/
function array_middle_insert() {
var arr = ['1','2','3','4','5','6'];
if(0 == arr.length % 2) { //偶數(shù)
arr.splice(arr.length / 2,0,'中間添加');
}
//["1","2","3","中間添加","4","5","6","7"]
console.log(JSON.stringify(arr));
}
1
2
3
4
5
6
7
8
9
10
11
數(shù)組對(duì)象方法的是使用介紹:
cancat()
/**
* concat:連接兩個(gè)或者多個(gè)數(shù)組偷卧,并且返回該數(shù)組
* 語法:array.concat(object,object,......);
*/
function array_concat() {
var arr = [{index:'0'},{address:'1'}];
var arr_1 = [{index:'3'},{index:'4'}];
var newArr = arr.concat(arr_1);
//[{"index":"0"},{"address":"1"},{"index":"3"},{"index":"4"}]
console.log(JSON.stringify(newArr));
}
1
2
3
4
5
6
7
8
9
10
11
12
join()
/**
* join():通過指定字符對(duì)數(shù)據(jù)進(jìn)行分割熔吗,返回字符串蚊惯。
* jon(分隔符);
*/
function array_join() {
var arr = ['1','2','3'];
var str = arr.join('-');
//1-2-3
console.log(str);
}
1
2
3
4
5
6
7
8
9
10
pop()
/**
* pop():刪除數(shù)組的最后一個(gè)對(duì)象愿卸,返回該刪除元素的值
*/
function array_pop() {
var arr = [{index:'0'},{address:'1'}];
var pop_data = arr.pop();
//{"address":"1"}
console.log(JSON.stringify(pop_data));
//[{"index":"0"}]
console.log(JSON.stringify(arr));
}
1
2
3
4
5
6
7
8
9
10
11
12
push()
/**
* push:向數(shù)組末尾添加一個(gè)或者多個(gè)對(duì)象。
* array.push(newObject1,newObject2,.....);
*/
function array_push() {
//實(shí)例化一個(gè)數(shù)組對(duì)象
var arr = new Array();
var obj_1 = { index:'1', index:'2'};
var obj_2 = { index:'3', index:'3'};
arr.push(obj_1,obj_2);
//[{"index":"2"},{"index":"3"}]
console.log(JSON.stringify(arr));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
shift()
/**
* shift():該方法用戶刪除數(shù)組的第一個(gè)對(duì)象截型,并返回刪除的元素
*/
function array_shift() {
var arr = [{index:'0'},{address:'1'}];
var shift_data = arr.shift();
//{"index":"0"}
console.log(JSON.stringify(shift_data));
//[{"address":"1"}]
console.log(JSON.stringify(arr));
}
1
2
3
4
5
6
7
8
9
10
11
slice()
/**
* slice():從已知數(shù)組中返回指定選中的數(shù)據(jù),該操作不會(huì)修改原數(shù)組的數(shù)據(jù)
* slice(start,end);
*/
function array_slice() {
var arr = ['1','2','3','4','5'];
var new_array = arr.slice(1,arr.length);
//["2","3","4","5"]
console.log(JSON.stringify(new_array));
//["1","2","3","4","5"]
console.log(JSON.stringify(arr));
}
1
2
3
4
5
6
7
8
9
10
11
12
sort()
/**
* sort():方法用戶對(duì)數(shù)組的排序
* sort(sortby) sortby可選趴荸,必須是函數(shù)。如調(diào)用方法沒有使用參數(shù)宦焦,則按字母順序進(jìn)行排序发钝。
*/
function array_sory() {
var arr = [{index:'500'},{index:'40'},{index:'100'},{index:'50'}];
var new_array = arr.sort(function(a,b) {
var n1 = Number(a.index);
var n2 = Number(b.index);
return n1 - n2;
});
//[{"index":"40"},{"index":"50"},{"index":"100"},{"index":"500"}]
console.log(JSON.stringify(new_array));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
splice()
/**
* 向數(shù)組中刪除/添加對(duì)象,并返回被刪除的元素波闹。
* splice(index,count,item1,item2,......);
* index:規(guī)定刪除/添加項(xiàng)目的起始位置酝豪,使用負(fù)數(shù)從數(shù)組的結(jié)尾處規(guī)定位置。
* count:規(guī)定要?jiǎng)h除項(xiàng)目的數(shù)量
* item1,item2...:向項(xiàng)目中添加添加新的對(duì)象
*/
function array_splice() {
var arr = ['1','2','3'];
var new_array = arr.splice(0,2,'4','5');
//["1","2"]
console.log(JSON.stringify(new_array));
//["4","5","3"]
console.log(JSON.stringify(arr));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
unshift()
/**
* 向數(shù)組開頭添加一個(gè)或者多個(gè)元素精堕,并返回新的長(zhǎng)度孵淘。
*/
function array_unshift() {
var arr = [{index:'0'},{address:'1'}];
var obj_1 = {index:'2'};
var obj_2 = {index:'3'};
var length = arr.unshift(obj_1,obj_2);
//4
console.log(length);
//[{"index":"2"},{"index":"3"},{"index":"0"},{"address":"1"}]
console.log(JSON.stringify(arr));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
toString():將數(shù)組轉(zhuǎn)換成字符串,并返回該結(jié)果歹篓。不會(huì)改變?cè)瓟?shù)組的數(shù)據(jù)瘫证。
toLocaleString():將數(shù)組裝換成本地字符,并返回結(jié)果庄撮,用“背捌,”分割。類似toString 方法洞斯。
reverse():該該方法顛倒數(shù)組的順序
一下舉栗聲明json數(shù)組串較為復(fù)雜并且靈活的格式载萌。
[
{
"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"
}
]
}
]
}
]