let arr = [100, 200, 300, 400, 500]
let sum = 0
// arr.forEach(function (item) {
// sum += item // item指的就是数组中的每一个数据
// })
arr.forEach((item) =>{
sum += item // item指的就是数组中的每一个数据
})
console.log(sum);
let arr = [100, 200, 300, 400, 500]
let sum = 0
for (var i = 0; i < arr.length; i++) {
sum += arr[i] // arr[i]就是数组中的每一个数据
}
const arr = [1,2,3,4]
// for ... in
for (const key in arr){
console.log(key) // 输出 0,1,2,3
}
// for ... of
for (const key of arr){
console.log(key) // 输出 1,2,3,4
}
const object = { name: 'mary', age: 23 }
// for ... in
for (const key in object) {
console.log(key) // 输出 name,age
console.log(object[key]) // 输出 mary,23
}
// for ... of
for (const key of object) {
console.log(key) // 报错 Uncaught TypeError: object is not iterable
}
const list = [{ name: 'mary' }, { age: 23 }]
for (const val of list) {
console.log(val) // 输出{ name: 'mary' }, { age: 23 }
for (const key in val) {
console.log(val[key]) // 输出 mary,23
}
}
1.语法: Object.assign(target,source1,source2,…)
2. 参数:
target 目标对象,接收源对象属性的对象,也是修改后的返回值。
sources 源对象,包含将被合并的属性。
3. 返回值:目标对象
4. 该方法主要用于对象的合并,将源对象source的所有可枚举属性合并到目标对象target上,此方法只拷贝源对象的自身属性,不拷贝继承的属性。实行的是浅拷贝,而不是深拷贝。也就是说,如果源对象某个属性的值是对象,那么目标对象拷贝得到的是这个对象的引用。同名属性会替换。
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
//同名属性会替换
const returnedTarget = Object.assign(target, source);
console.log(target);
// { a: 1, b: 4, c: 5 }
console.log(returnedTarget);
// { a: 1, b: 4, c: 5 }
let arr = ["a", "b", "c"];
console.log(Object.keys(arr));
// ['0', '1', '2']
/* Object 对象 */
let obj = { foo: "bar", baz: 42 },
keys = Object.keys(obj);
console.log(keys);
// ["foo","baz"]
var an_obj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.values(an_obj)); // ['b', 'c', 'a']
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.values(obj)); // ['a', 'b', 'c']
let list= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let evenList = list.filter(num => num % 2 === 0);
// 过滤出所有偶数
console.log(evenList); // 输出: [2, 4, 6, 8, 10]
let list= [
{
name:'张三',
type:1,
},
{
name:'李四',
type:2,
}
];
// let tempList = list.filter(item=> item.type == 1);
let tempList = list.filter((item,index)=> {
return item.type == 1
});
// 过滤出所有type等于1的数据
console.log(tempList); // 输出: [{name:'张三',type:'1'}]
let list= [1, 2, 3, 4, 5];
let squaredList = list.map(num => num * num);
// 对每个元素进行平方操作
console.log(squaredList); // 输出: [1, 4, 9, 16, 25]
let list= [
{
name:'张三',
type:1,
},
{
name:'李四',
type:2,
}
];
// let nameList = list.map(item=> item.name);
// let typeList = list.map(item=> item.type);
let nameList = list.map((item,index)=> {
return item.name
});
let typeList = list.map((item,index)=> {
return item.type
});
console.log(nameList); // 输出: ['张三','李四']
console.log(typeList); // 输出: [1,2]
"2:3:4:5".split(":")//将返回["2", "3", "4", "5"]
"|a|b|c".split("|")//将返回["", "a", "b", "c"]
"hello".split("")//可返回 ["h", "e", "l", "l", "o"]
"hello".split("", 3)//可返回 ["h", "e", "l"]
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var energy = fruits.join();
//将返回 Banana,Orange,Apple,Mango
const names = ['张三', '李四', '王五', '赵六'];
const slicedNames = names.slice(1, 3);
const slicedNames1 = names.slice();
const slicedNames2 = names.slice(0);
const slicedNames3 = names.slice(2);
const slicedNames4 = names.slice(3);
const slicedNames5 = names.slice(4);
//slice不改变原数组
console.log(names); //[ '张三', '李四', '王五', '赵六' ]
console.log(slicedNames); //[ '李四', '王五' ]
console.log(slicedNames1); //[ '张三', '李四', '王五', '赵六' ]
console.log(slicedNames2); //[ '张三', '李四', '王五', '赵六' ]
console.log(slicedNames3); //[ '王五', '赵六' ]
console.log(slicedNames4); //[ '赵六' ]
console.log(slicedNames5); //[] 参数大于等于4时就输出空数组
//实现删除
let arr = [1,2,3,4,5]
console.log(arr.splice(0,2)); //[ 1, 2 ] 返回被删除的元素
console.log(arr); //[ 3, 4, 5 ] 该方法会改变原数组
//实现添加
let arr2 = [1,2,3,4,5]
console.log(arrsplice(1,0,666,777)); //[] 返回空数组,没有删除元素
console.log(arr2); //[ 1, 666, 777, 2, 3, 4, 5 ] 原数组改变了
// 实现替换
let arr3 = [1,2,3,4,5]
console.log(arrsplice(2,1,"aaa","bbb")); //[ 3 ] 返回被删除的一个元素
console.log(arr3); //[ 1, 2, 'aaa', 'bbb', 4, 5 ] 可以添加字符串
let arr4 = [1,2,3,4,5]
console.log(arrsplice(1,4,666)); //[ 2, 3, 4, 5 ] 返回被删除的四个元素
console.log(arr4); //[ 1, 666 ]
const fruits1 = ['苹果', '橘子'];
const fruits2 = ['西瓜', '草莓'];
const combinedFruits = fruitsconcat(fruits2);
console.log(combinedFruits); //[ '苹果', '橘子', '西瓜', '草莓' ]
const fruits = ['苹果', '香蕉'];
const res = fruits.push('橘子', '西瓜');
console.log(fruits); //[ '苹果', '香蕉', '橘子', '西瓜' ]
console.log(res); //4
const fruits = ['苹果', '香蕉', '橘子'];
const lastFruit = fruits.pop();
console.log(fruits); // ['苹果', '香蕉']
console.log(lastFruit); //橘子
const fruits = ['苹果', '香蕉'];
const newLength = fruits.unshift('橘子', '西瓜');
console.log(fruits); //[ '橘子', '西瓜', '苹果', '香蕉' ]
console.log(newLength); //4
const fruits = ['苹果', '香蕉', '橘子'];
const firstFruit = fruits.shift();
console.log(fruits); //[ '香蕉', '橘子' ]
console.log(firstFruit); //苹果