数组

2017-08-30  本文已影响16人  YM雨蒙

创建数组

var array = new Array()
var array =[1,2,3,true,'yym']

arr.length

var students = [
  {id:1,score:80},
  {id:2,score:70},
  {id:3,score:90}
]
students.length  //3
students = []
students.length  //0

获取数组元素

var students = [
  {id:1,score:80},
  {id:2,score:70},
  {id:3,score:90}
]
students[0]  //{id: 1, score: 80}
students[1].score = 60  //60  修改

arr.indexOf(searchElement)

var tel = [110,114,120]
tel.indexOf(110)  //0 索引位置
tel.indexOf(119)  //-1 没有返回-1

arr.forEach(callback)遍历

当前元素
当前元素索引值
整个数组
var students = [
    {id:1,score:80},
    {id:2,score:50},
    {id:3,score:70}
];
var editScore = function(item,index,array){
    item.score += 5;
};
students.forEach(editScore);


var scores = [60,70,80,90]
var newScores = []
var editScores = function(item,index,array){
  newScores.push(item+5)
}
scores.forEach(editScores)
console.log(newScores)

arr.reverse()

var students = [
    {id:1,score:80},
    {id:2,score:50},
    {id:3,score:70}
];
students.reverse()

[{id: 3, score: 70}
{id: 2, score: 50}
{id: 1, score: 80}]

arr.sort([comparaFunction])

var students = [
    {id:1,score:80},
    {id:2,score:50},
    {id:3,score:70}
];
var byScore = function(a,b){
  return b.score - a.score
}
students.sort(byScore)
// {id: 1, score: 80}
// {id: 3, score: 70}
// {id: 2, score: 50}

arr.push(element1,element2...)

var students = [
    {id:1,score:80},
    {id:2,score:50},
    {id:3,score:70}
];
students.push({id:4,score:90})  //4

var a = new Array(1,2,3);
a.push(4);
console.log(a);//[1, 2, 3, 4]

arr.unshift(element1,element2...)

var a=new Array(1,2,3);
a.unshift(4);
console.log(a);//[4, 1, 2, 3]

arr.shift()

var students = [
    {id:1,score:80},
    {id:2,score:50},
    {id:3,score:70}
];

students.shift()  //{id: 1, score: 80}

var a=new Array(1,2,3);
a.shift();  //1
console.log(a); //[2, 3]

arr.pop

var students = [
    {id:1,score:80},
    {id:2,score:50},
    {id:3,score:70}
];
students.pop()  //{id: 3, score: 70}

var a = new Array(1,2,3);
a.pop();  //3
console.log(a); //[1, 2]

arr.splice(index,howmany[,elements])

替换

删除

添加

arr.reverse() arr.sort() arr.push() arr.unshift() arr.shift() arr.pop() arr.splice() 都改变了数组


arr.slice(begin[,end])

var num = [1,2,3,4,5,6]
num.slice(1,4)  // [2, 3, 4]
num // [1, 2, 3, 4, 5, 6]

arr.concat(value1,value2,...)

var a = [1,2,3,4,5];
var b = [6,7,8,9];
console.log(a.concat(b));//[1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(a); //[1, 2, 3, 4, 5]
console.log(b); //[6, 7, 8, 9]

arr.join(separator)

var a = [1,2,3,4,5];
console.log(a.join(',')); //1,2,3,4,5
console.log(a.join('-')); //1-2-3-4-5

arr.map(callback)

var scores = [60,70,80,90]
var addScore = function(item,index,array){
  return item+5
}
scores.map(addScore)

arr.reduce(callback,[initialValue])

var students = [
    {id:1,score:80},
    {id:2,score:50},
    {id:3,score:70}
];
var sum = function(previousResult,item,index,array){
    return previousResult+item.score;
};
students.reduce(sum,0);  // 200

增加的一些数组知识

Array.isArray(obj)

var a = [];
var b = new Date();
console.log(Array.isArray(a)); //true
console.log(Array.isArray(b)); //false

arr.lastIndexOf(element)

var a = [1,2,3,3,2,1];
console.log(a.indexOf(2)); //1
console.log(a.lastIndexOf(2)); //4

.every(function(element, index, array)) / .some(function(element, index, array))

var a=new Array(1,2,3,4,5,6);

console.log(a.every(function(e, i, arr){
return e < 5;
}));  //false

console.log(a.some(function(e,i,arr){
  return e > 4;
}));  //true

.filter(function(element))(类似于过滤)

var a = new Array(1,2,3,4,5,6);

console.log(a.filter(function(e){
  return e % 2 == 0;
})); // [2, 4, 6]

console.log(a); //[1, 2, 3, 4, 5, 6]
上一篇 下一篇

猜你喜欢

热点阅读