Object.values()对象遍历
Object.keys() 对象的遍历
返回给定对象所有可枚举属性的数组;是属性名组成的数组
let obj = { a: 1, b: 2, c: 3 }; Object.keys(obj).map((key) => { console.log(key, obj[key]); });
Object.values() 对象的遍历
返回一个给定对象自身的所有属性值的数组; 是属性值组成的数组
let obj22 = { xe: { name: "小红", age: "19", height: "166" }, xe2: { name: "小张", age: "20", height: "176" }, xe3: { name: "小洋", age: "21", height: "168" }, }; const list0 = Object.values(obj22); // console.log('d', list0) // [ {name: '小红', age: '19', height: '166'}, // {name: '小张', age: '20', height: '176'}, // {name: '小洋', age: '21', height: '168'}]
Object.entries() 对象的遍历
Object.entries() 可以把一个对象的键值以数组的形式遍历出来,结果和 for...in 一致,但不会遍历原型属性
const obj = { foo: 'bar', baz: 'abc' }; console.log(Object.entries(obj)); // [['foo', 'bar'], ['baz', 'abc']]
let obj3 = { foot:'111', heder:'222' } const list33 = Object.entries(obj3); for (let [k, v] of Object.entries(obj3)) { console.log('值', k +':'+ v) }
对象的遍历: for in
let obj2 = { name: "xiaohong", id: "001", age: 19 }; for (const key in obj2) { if (Object.hasOwnProperty.call(obj2, key)) { console.log(key, obj2); const element = obj2[key]; } }
some()
根据传入的条件有一个符合条件,some ()返回 true。如果没有,它将返回false
const stock = { meat: 5, fruit: 10, cheese: 6, } const enoughStock = Object.values(stock).some(el => el >= 6) console.log(enoughStock) // true
every()
Every ()根据条件返回 true 或 false,如果每一项都符合条件
const nightclub = { dave: 26, sally: 23, lucy: 17, } const isOldEnough = Object.values(nightclub).every(el => el >= 18) // false if (!isOldEnough) { console.log("Check every ID") }
检测数组中的每一项是否符合要求
const comp = [ {name:"Apple",ram:8}, {name:"IBM",ram:4}, {name:"Acer",ram:32}, ]; var result= comp.every(function(computer){ return computer.ram > 16 }) console.log(result)//false;
map, filter, some, forEach, for in, for of, 用法
1, forEach
对数组中的每一个元素进行遍历,没有返回值,无法使用break,return等终止循环
let list4 = [1, 2, 3, 4, 5]; let other4 = []; list4.forEach((item, i) => { other4.push(item * 2); });
2, map
有返回值,返回一个新的数组
let list1 = [1, 2, 3, 4, 5]; let other1 = list1.map((item, i) => { return item * 2; });
3, filter
有返回值,返回一个符合条件的元素 组成的数组
let list2 = [1, 2, 3, 4, 5]; let other2 = list2.filter((item, i) => { return item % 2; });
4, some
遍历数组中的每一个元素
返回一个boolean,判断是否有元素符合条件,如果有一个符合条件,则循环会终止。
let list3 = [1, 2, 3, 4, 5]; list3.some((d, i) => { // console.log('some遍历数组',d, i); return d > 3; });
5,reduce() 返回一个值
prev 表示上一次调用返回的结果值,或初始值init
cur 表示当前的数组元素
init 表示初始值
// 求和 let arr = [3,9,6,2,9] let sum = arr.reduce((prev, cur) => { return prev + cur; }, 0) // init 初始值为0, 所以开始时prev的值为0 // 数组去重 let newArr = arr.reduce((prev, cur) => { prev.indexOf(cur) == -1 && prev.push(cur) return prev; }, [])
6, for of
可遍历数组,获取的是value值;可以代替for循环,用下标索引来获取value值
for of为ES6提供,遍历范围包括数组、Set和Map结构,dom树,arguments对象,以及字符串
// 遍历数组 let list5 = [1, 2, 3, 4, 5]; for (let item of list5) { // console.log('for of',item); } let arr3 = [ { key: "01", value: "西施" }, { key: "02", value: "王昭君" }, { key: "03", value: "杨玉环" }, { key: "02", value: "王昭君" }, ]; function newObj(arr) { let newarr = []; for (let i = 0; i < arr.length; i++) { let isBol = newarr.some((item) => item.key == arr[i].key); // 如果找到没有就添加到新数组 if (!isBol) { newarr.push(arr[i]); } } return newarr; } console.log("对象", newObj(arr3));
https://blog.csdn.net/sunyctf/article/details/134491546
https://juejin.cn/post/7340554546255003663
https://juejin.cn/post/6967293500572303367?searchId=20240511165914D6D9BC19E90112C71A3A
https://juejin.cn/post/7017968328694038536
https://juejin.cn/post/6844903796062191624?searchId=20240511174205C6448560088235C02833