本篇文章给大家分享的是有关JavaScript中怎么对数组进行操作,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
js对数组对象的操作以及方法的使用
如何声明创建一个数组对象:
var arr = new Array();
或者
var arr = [];
如何移除所有数组中数据?
arrayJson.dataList.splice(0,arr.length);
如何指定位置对数组对象的添加,如何将指定元素添加到数组中间位置?
function array_middle_insert() {
var arr = ['1','2','3','4','5','6'];
if(0 == arr.length % 2) { //偶数
arr.splice(arr.length / 2,0,'中间添加');
}
//["1","2","3","中间添加","4","5","6","7"]
console.log(JSON.stringify(arr));
}
数组对象方法的是使用介绍:
cancat()
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));
}
join()
function array_join() {
var arr = ['1','2','3'];
var str = arr.join('-');
//1-2-3
console.log(str);
}
pop()
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));
}
push()
function array_push() {
//实例化一个数组对象
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));
}
shift()
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));
}
slice()
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));
}
sort()
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));
}
splice()
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));
}
unshift()
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));
}
toString():将数组转换成字符串,并返回该结果。不会改变原数组的数据。
toLocaleString():将数组装换成本地字符,并返回结果,用“,”分割。类似toString 方法。
reverse():该该方法颠倒数组的顺序
一下举栗声明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中怎么对数组进行操作,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网行业资讯频道。