师傅喊我自己学习。。。呜呜呜呜(难过)
是不是我太菜了师傅都不喊我做了,他自己做快多了。
自己给自己安排吧。
1、在demo里面用一下vuex吧 (x)
2、把demo的权限管理写完吧 (x)
3、看边缘云项目,看看他的功能是如何实现的,在demo里面模仿 (x)
自主学习+1
但咱们好好安排一下:
1、首先,把mdn的数组的方法都看一下,敲一下(上午)
2、下午学习vue,自己写demo练习
3、看公司项目(路由配置、首页、封装组件、第一个菜单页面)
const fruits = [];
fruits.push('banana', 'apple', 'peach');
fruits[5] = 'mango';
console.log(fruits[5]); // 'mango'
console.log(Object.keys(fruits)); // ['0', '1', '2', '5']
console.log(fruits.length); // 6
//数组的属性 length
//练习:1、遍历数组
let numbers = [1, 2, 3, 4, 5];
/* for(let i=0;i<numbers.length;i++){
numbers[i]*=numbers[i]
}
console.log(numbers) */ //[ 1, 4, 9, 16, 25 ]
//练习2截断数组
if (numbers.length > 3) {
numbers.length = 3;
}
console.log(numbers); //[ 1, 2, 3 ]
//数组的方法
//1、at()
/* const cart = ['apple', 'banana', 'pear'];
console.log(cart.at(-1))
function returnLast(arr){
return arr.at(-1);
}
const item1=returnLast(cart)
console.log(item1) */
//2、concant
var alpha = ['a', 'b', 'c'];
var numeric = [1, 2, 3];
let num = [4, 5, 6];
console.log(alpha.concat(numeric)); //[ 'a', 'b', 'c', 1, 2, 3 ]
console.log(alpha.concat(5)); //[ 'a', 'b', 'c', 5 ]
console.log(alpha.concat()); //[ 'a', 'b', 'c' ]
console.log(alpha.concat(numeric, [11, 33])); //['a', 'b', 'c', 1, 2, 3, 11, 33 ]
//every
//检测数组是否所有元素都大于10
function isBigger(num) {
if (num > 10) return true;
return false;
}
//[1,11,1,2,13].every(isBegger(num))
console.log([1, 11, 1, 2, 13].every(isBigger)); //false
console.log(
[1, 11, 1, 2, 13].every(function (num) {
if (num > 10) return true;
return false;
})
); //false
console.log([].fill.call({ length: 3 }, 4));
//filter 筛选数组中大于10的值
let arrs5 = [1, 11, 1, 2, 13, 55, 100];
let res = arrs5.filter((val) => {
if (val > 10) return true;
return false;
});
console.log(res); //[ 11, 13, 55, 100 ]
console.log(arrs5); //[1, 11, 1, 2, 13,55,100]
//使用 filter() 根据搜索条件来过滤数组内容。
var fruits2 = ['apple', 'banana', 'grapes', 'mango', 'orange'];
function myFilter(val) {
return fruits2.filter((fruit) => {
return fruit.indexOf(val) > -1;
});
}
console.log(myFilter('ap')); //[ 'apple', 'grapes' ]
//find
//用对象的属性查找数组里的对象
const inventory = [
{ name: 'apples', quantity: 2 },
{ name: 'bananas', quantity: 0 },
{ name: 'cherries', quantity: 5 }
];
function isCherries(val) {
return val.name === 'cherries';
}
console.log(inventory.find(isCherries)); //{ name: 'cherries', quantity: 5 }
//从数组中寻找质数
function isPrime(val) {
let n = 2;
while (n <= Math.sqrt(val)) {
if (val % n++ < 1) {
return false;
}
}
return val > 1;
}
console.log([5, 2, 4, 6, 7, 9].find(isPrime));
//利用flat()扁平化数组
var arr1 = [1, 2, [3, 4]];
console.log(arr1.flat()); //[ 1, 2, 3, 4 ]
console.log(arr1.flat(0)); //[ 1, 2, [ 3, 4 ] ]
var arr2 = [1, 2, [3, 4, [5, 6]]];
console.log(arr2.flat()); //[ 1, 2, 3, 4, [ 5, 6 ] ]
console.log(arr2.flat(2)); //[ 1, 2, 3, 4, 5, 6 ]
//使用 Infinity,可展开任意深度的嵌套数组
var arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
console.log(arr4.flat(Infinity)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
//flat()方法会移除数组里面的空项,可以用reduce与concat代替
let arr7 = [1, 2, [3, 4]];
let res2 = arr7.reduce((acc, arr) => {
return acc.concat(arr); //concat可以连接值和数组
}, []);
console.log(res2); //[ 1, 2, 3, 4 ]
//使用扩展运算符的写法
let arr8 = [1, 2, 3, [1, 2, 3, 4, [2, 3, 4]]];
let res3 = [].concat(...arr8);
console.log(res3); //[ 1, 2, 3, 1, 2, 3, 4, [ 2, 3, 4 ] ]
//使用 reduce、concat 和递归展开无限多层嵌套的数组
//用forEach来实现扁平化数组
const problem = [1, 2, 3, [4, 5, [6, 7], 8, 9]];
function flatten(arr) {
const res = [];
arr.forEach((i) => {
if (Array.isArray(i)) {
res.push(...i); //[ 1, 2, 3, 1, 2, 3, 4, [ 2, 3, 4 ] ]
res.push(...flatten(i)); //需要递归调用 [ 1, 2, 3, 4, 5, 6, 7, 8, 9]
} else {
res.push(i);
}
});
return res;
}
console.log(flatten(problem));
//Array.from 实现数组去重合并
function combine() {
let arr = [].concat(...arguments);
//let arr = [].concat.apply([], arguments); 效果相同
return Array.from(new Set(arr));
}
var m = [1, 2, 2],
n = [2, 3, 3];
console.log(combine(m, n)); //[ 1, 2, 3 ]
console.log([1, 2, 3].includes(2)); //true
//找出指定元素出现的所有位置
var indices = [];
var array = ['a', 'b', 'a', 'c', 'a', 'd'];
var element = 'a';
let position = array.indexOf(element);
while (position != -1) {
indices.push(position);
position = array.indexOf(element, position + 1);
}
console.log(indices); //[ 0, 2, 4 ]
//map
var numbers1 = [1, 4, 9];
let res4 = numbers1.map((item) => item * item);
console.log(res4); //[ 1, 16, 81 ] //可以用Math.sqrt
var kvArray = [
{ key: 1, value: 10 },
{ key: 2, value: 20 },
{ key: 3, value: 30 }
];
function reArray(kvArray){
return kvArray.map((Array)=>{
let res={}; //只用先考虑每一条
res[Array.key]=Array.value
return res
})
}
console.log(reArray(kvArray)) //[ { '1': 10 }, { '2': 20 }, { '3': 30 } ]