数组的栈方法和队列方法
<!DOCTYPE html>
<html lang="UTF-8">
<head>
<meta charset="UTF-8">
<title>数组常用的方法</title>
</head>
<body>
<script type="text/javascript">
let colors = ['red', 'blue', 'green'];
let a = colors.join('|');
console.log(a);
let newlength = colors.push('orange');
console.log('The length is '+newlength);
console.log(colors);
let lastItem = colors.pop();
console.log(lastItem);
console.log(colors);
newlength = colors.unshift('yellow');
console.log(newlength);
console.log(colors);
console.log(firstItem);
console.log(colors);
</script>
</body>
</html>
数组的排序
<!DOCTYPE html>
<html lang="UTF-8">
<head>
<meta charset="UTF-8">
<title>数组方法</title>
</head>
<body>
<script type="text/javascript">
let values = [0, 2, 10, 8, 7, 5, 1];
values.reverse();
console.log(values);
function compareUp(a, b) {
return a - b;
}
function compareDown(a, b){
return b - a;
}
values.sort(compareUp);
console.log(values);
values.sort(compareDown);
console.log(values);
</script>
</body>
</html>
数组的操作方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数组的操作方法</title>
</head>
<body>
<script type="text/javascript">
let colors = ['red', 'green'];
let newColors = colors.concat('blue');
console.log(newColors);
newColors = colors.concat('blue',{name:"Mike",age:18},['orange', 'pink']);
console.log(newColors);
newColors = newColors.slice(1);
console.log(newColors);
newColors = newColors.slice(1,3);
console.log(newColors);
newColors = newColors.slice(-2,-1);
console.log(newColors);
let names = ['zhangsan', 'lisi', 'wangwu', 'liliu'];
names.splice(0,2);
console.log(names);
names.splice(1,0,'sunwukong', 'zhubajie');
console.log(names);
names.splice(0,1,'王小二');
console.log(names);
</script>
</body>
</html>
## 数组的迭代方法
数组的迭代方法
<!DOCTYPE html>
<html lang="UTF-8">
<head>
<meta charset="UTF-8">
<title>数组的迭代方法</title>
</head>
<body>
<script type="text/javascript">
let numbers = [1,2,3,11,12,13,20,99];
let filterResult = numbers.filter(function (item, index, array) {
return item > 10;
});
console.log(filterResult);
let mapResult = numbers.map(function (item, index, array) {
return item * 2;
});
console.log(mapResult);
numbers.forEach(function (item, index) {
console.log(index+": "+item);
});
for (let i = 0; i < numbers.length; i++){
console.log(numbers[i]);
}
</script>
</body>
</html>
map方法的应用
<!DOCTYPE html>
<html lang="UTF-8">
<head>
<meta charset="UTF-8">
<title>map方法的应用</title>
</head>
<body>
<script type="text/javascript">
let oldArray = [
{
name:'张三',
age:17
},
{
name:'李四',
age:18
},
{
name:'王五',
age:20
}
];
let newNames = oldArray.map(function (item, index) {
return item.name;
});
let newAges = oldArray.map(function (item, index) {
return item.age;
});
console.log(newNames);
console.log(newAges);
</script>
</body>
</html>