一.push 是用来做什么的? 以此数组为例: sum=[12,34,56,78,89,50],写代码示范;
var num = [1, 2, 3, 4, 5, 6, 7, 8]
num.push(9)
onsole.log(num)
二.forEach的作用是什么?使用下列数组计算每个人的年龄?
var arr = [
{name: '小王',age: 50},
{name: '小李',age: 60},
{name: '小林',age: 85},
{name: '小明',age: 95}]
#
var sum = 0;
arr.forEach(tiem => {
sum += tiem.age;
})
console.log(sum)
三.map 简写的作用?
var s = [
{ nme: "张飞", age: 15 },
{ nme: "关羽", age: 15 },
{ nme: "刘备", age: 15 }
]
var x = s.map(function (tiem) {
return {
nme: tiem.nme,
age: tiem.age + 10
}
})
console.log(x)
四.splice是用来做什么的,并自己举例?
var s = [1, 2, 3, 4, 5]
s.splice(2, 1)
console.log(s)
s.splice(2, 0, "大家好")
console.log(s)
五.filter 是用来做什么的? 将下列数据中的isHot为1的字段筛选出来:
var hotCity = [
{ "cityId": 110100,"name": "北京", "pinyin": "beijing", "isHot": 0 },
{ "cityId": 310100, "name": "上海", "pinyin": "shanghai", "isHot": 1 },
{ "cityId": 440100, "name": "广州", "pinyin": "guangzhou","isHot": 0 },
{ "cityId": 440300, "name": "深圳", "pinyin": "shenzhen", "isHot": 1 }
],
#
var x = hotCity.filter(function (tiem) {
return tiem.isHot >= 1
})
console.log(x)
六.说说 find 和 findIndex 的相同点和不同点.
find返回条件的数组元素头
findlndex返回后数组元素尾
七.将下列所有的数组方法,用代码运行一边,并写成简书 .
- push 向数组的末尾添加一个或更多元素,并返回新的长度。
var num = [1, 2, 3, 4, 5, 6, 7, 8]
num.push(9)
onsole.log(num)
2.unshift 向数组的开头添加一个或更多元素,并返回新的长度。
var s = [1, 2, 3]
s.unshift(0)
console.log(s)
3.pop 删除数组的最后一个元素并返回删除的元素。
var s = [1, 2, 3]
s.pop()
console.log(s)
4.shift 删除并返回数组的第一个元素(知道即可,用得很少)。
var s = [1, 2, 3]
s.shift()
console.log(s)
- reverse 反转数组的元素顺序。
var s = [1, 2, 3]
s.reverse(2)
console.log(s)
- find 返回符合传入测试(函数)条件的数组元素。
var s = [
{ name: "小红", age: 15 },
{ name: "小狗", age: 25 },
{ name: "小猪", age: 35 },
{ name: "小王", age: 45 }
]
var x = s.find(function (age) {
return age.name === '小红';
})
console.log(x)
- findIndex 返回符合传入测试(函数)条件的数组元素索引。
var s = [
{ name: "小红", age: 15 },
{ name: "小狗", age: 25 },
{ name: "小猪", age: 35 },
{ name: "小王", age: 45 }
]
var x = s.findIndex(tiem => {
return tiem.name === '小猪';
})
console.log(x)
- slice 选取数组的的一部分,并返回一个新数组。
var s = [1, 2, 3, 4, 5];
// var x = s.slice(2)
// console.log(x)
var x = s.slice(2, 4)
console.log(x)
- splice 从数组中添加或删除元素。
var s = [1, 2, 3, 4, 5]
s.splice(2, 1)
console.log(s)
s.splice(2, 0, "大家好")
console.log(s)
- join 把数组的所有元素放入一个字符串。
var s = ['a', 'b', 'c'];
var x = s.join("-");
console.log(x)
11.forEach 迭代数组
var arr = [
{ name: '小王', age: 50 },
{ name: '小李', age: 60 },
{ name: '小林', age: 85 },
{ name: '小明', age: 95 }
]
var sum = 0;
arr.forEach(tiem => {
sum += tiem.age;
})
console.log(sum)
- map 通过指定函数处理数组的每个元素,并返回处理后的数组。
var s = [
{ nme: "张飞", age: 15 },
{ nme: "关羽", age: 15 },
{ nme: "刘备", age: 15 }
]
var x = s.map(function (tiem) {
return {
nme: tiem.nme,
age: tiem.age + 10
}
})
console.log(x)
- filter 检测数值元素,并返回符合条件所有元素的数组。
var hotCity = [ { "cityId": 110100, "name": "北京", "pinyin": "beijing", "isHot": 0 }, { "cityId": 310100, "name": "上海", "pinyin": "shanghai", "isHot": 1 }, { "cityId": 440100, "name": "广州", "pinyin": "guangzhou", "isHot": 0}, { "cityId": 440300, "name": "深圳", "pinyin": "shenzhen", "isHot": 1} ] var x = hotCity.filter(function (tiem) { return tiem.isHot >= 1 }) console.log(x)
- indexOf 搜索数组中的元素,并返回它所在的位置。
var s = ['a', 'b', 'c', 'd'];
var x = s.indexOf('b');
console.log(x)
- includes 判断一个数组是否包含一个指定的值
var s = ['a', 'b', 'c', 'd'];
var x = s.includes('c')
var num = s.includes('g')
console.log(x)
console.log(num)
- isArray 判断对象是否为数组。
var num = { age: 2 };
var x = Array.isArray(num);
console.log(x)
var sum = [1, 2, 4, 5];
var s = Array.isArray(sum);
console.log(s)
17.sort 数组排序
var s = [1, 2, 3, 4, 5]
s.sort();
console.log(s)
s.sort(function (a, b) {
return b - a
})
console.log(s)