给数组添加元素
var arr = ['a','b','c','d']
arr.push('q')
移除数组的第一个元素
var arr = ['a','b','c','d']
array2=arr.shift() // 'a'
console.log(arr) ['b','c','d']
移除数组最后一个元素
var arr = ['a','b','c','d']
array2=arr.pop() // 'd'
console.log(arr) ['a','b','c']
删除数组中的某个元素 splice
let arr = ['a','b','c','d']
arr.splice('b',1); //不对,不可以用内容来指定
arr.splice(1,1); //这个输出的是["b"]
console.log(arr)//这个输出来是["a","c","d"]
arr.slice(1,3)//['b','c']slice是返回指定位置的元素,注意区分,这个不会改变原数组
let arr = ['a','b','c','d']
//$.each()遍历
$.each(arr, function(key, value){
console.log(key+'...'+value)
//for遍历
for(let i = 0; i < arr.length; i++){
console.log(arr[i])
数组筛选,注意参数的数组和$.each()正好相反
//找个简单的例子:
$.grep( [0,1,2], function(n,i){
return n > 0;
});//返回的结果是[1,2]
接下来比较数组筛选的几种不同方法
var nums = '1,2,3,4,5,jQuery,CSS,5'.split(',');
//$.grep()
nums = $.grep(nums, function (value, key) {
// value = 数组元素的当前值
// key = 当前值的下标
return isNaN(value);
console.log(nums); //结果为: ["jQuery", "CSS"]
//$.map()
nums = $.map(nums, function (value, index) {
//和$.grep() 的区别
//return isNaN,得到结果为:[true, true]
return isNaN(value) ? value : null;
console.log(nums); // ["jQuery", "CSS"]
var arr = ['a','b','c','d']
var arr2 = ['d','e','f','g']
let mergeArr = $.merge(arr,arr2 ) //['a','b','c','d','d','e','f','g']
var a=[1,2,3];
var b=['a','b','c'];
var c=["x","y","z"];
var js=a.concat(b,c);
console.log(js)//Array [ 1, 2, 3, "a", "b", "c", "x", "y", "z" ]
console.log(js.join("|"))// 1|2|3|a|b|c|x|y|z
//直接对数组去重
let yearArr = [2009,2009,2010,2010,2009,2010]
$.unique(yearArr) // [2009,2010,2009,1010]
//数组排序后去重
let yearArr = [2009,2009,2010,2010,2009,2010]
yearArr.sort()
$.unique(yearArr) //[2009,2010]
let yearArr = [2009,2009,2010,2010,2009,2010];
Array.from(new Set(yearArr)) //[2009,2010]
yearArr = [2009,2009,2010,2010,2009,2010];
console.log(unique2(yearArr))
function unique2(arr) {
var ret = [];
arr.forEach(function(item){
if(!ret.includes(item)){
ret.push(item);
return ret;
查找数组中元素的位置,从0开始
var arr = [ 4, "Pete", 8, "John" ];
$.inArray("John", arr); //3
$.inArray(4, arr); //0
$.inArray("David", arr); //-1
把数组拼接成字符串
var a=new Array();
a[0]="XHTML";
a[1]="CSS";
a[2]="JavaScript";
console.log(a.join());//XHTML,CSS,JavaScript
join()
join("#")可以加符号
将字符串分割成数组,与上面正好相反
let str = 'html,css,js,jquery'
let arr = str.split(',')
console.log(arr)//['html','css','js','jquery']
颠倒数组中的元素
var a=["xhtml","css","javascript"];
var b=a.reverse();
console.log(a);//Array [ "javascript", "css", "xhtml" ]
console.log(b);//Array [ "javascript", "css", "xhtml" ]
返回指定位置的数组 slice
let arr = ['a','b','c','d']
let b = arr.slice(1,3)
console.log(arr)//Array [ "a" ,"b","c","d"]
console.log(b)//Array [ "b", "c" ]从下标为1开始,到3结束,不包括3的位置
对象数组的操作
var obj = [
{'id':'1','name':'name1','age':'21'},
{'id':'2','name':'name2','age':'22'},
{'id':'3','name':'name3','age':'23'},
{'id':'4','name':'name4','age':'24'}
对象数组合并
var settings = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
$.extend(settings, options);
//输出settings == { validate: true, limit: 5, name: "bar" }
找到对象数组中的对应元素,然后删除
var list = [
{'id':'1','name':'name1','age':'21'},
{'id':'2','name':'name2','age':'22'},
{'id':'3','name':'name3','age':'23'},
{'id':'4','name':'name4','age':'24'}
function opt(){
console.log(list);
$.each(list, function (key,val) {
if(val.id == '3'){
console.log("finded...")
list[key] = ''
list.splice($.inArray('',list),1);
console.log(list)
// opt()这个方法是先遍历,找到需要删除的那条记录,然后将它变成'',再通过splice来删除
后来发现splice并不需要这么麻烦,可以直接用索引来删除
function opt2(){
console.log(list);
for(var i = 0 ;i < list.length; i++){
console.log(i)
if(list[i].id == '2'){
list.splice(i,1);