vue是数据驱动,vue主要操作的是数据 1、JS中有哪些数据类型 (1)基本数据类型:number,string,boolean,null,undefined (2)引用数据类型: Object, function, Symbol(ES6) 2、{} 和 [] 操作数组的方法有哪些: ES4: pop,push,unshfit,shfit,slice,splice,reverse,sort,indexOf,lastIndexOf,concat (pop push unshift shift splice reverse sort ) 括号中的能改变原数组,叫数组的变异 3、用的比较多的方法 forEach,filter,map,find,some,every,includes,reduce some,every 是 ES5的 ES6:(includes,find),其余都是ES5的 filter(过滤),map(映射) 4、node版本 版本最好升级到 8.5以上 5、for for循环和forEach是等价的,都是循环数组 例子:
1 2 3 4 5 6 7 8 9 |
let arr = [1, 2, 3, 4, 5]; for (let i = 0; i < arr.length; i++) { console.log(arr[i]); } |
5、forEach 例子:
1 2 3 4 5 6 7 8 9 |
let arr = [1, 2, 3, 4, 5]; arr.forEach(function (item, index) { console.log(item); }); |
注意:forEach不支持return 6、 for of for of 支持return,并且是只能遍历数组,不能遍历对象 for of 是ES6语法 例子:
1 2 3 4 5 6 7 8 9 |
let arr = [1, 2, 3, 4, 5]; for(let val of arr) { console.log(val); } |
7、如何用for of 遍历对象呢? 本来for of 不支持遍历对象。 Object.keys将对象的key作为新的数组 例子:
1 2 3 4 5 6 7 8 9 |
let obj = {school: 'name', age: 8}; for (let val of Object.keys(obj)) { console.log(obj[val]); } |
8、filter filter是过滤的意思,filter不会操作原数组,返回的是过滤后的新数组。 回调函数返回的结果,如果返回true,表示这一项放到新数组中。 例子: 过滤出来大于2,小于5的值
1 2 3 4 5 6 7 8 9 |
let newAry = [1, 2, 3, 4, 5].filter(function (item, index) { //index是索引 return item > 2 && item < 5; }); console.log(newAry); // [3, 4] |
9、map map是映射的意思,将一个数组映射成一个新数组。 map不操作原数组,返回一个新数组。 回调函数中返回什么,这一项就是什么。 例子: [1,2,3] 映射成 <li>1</li><li>2</li><li>3</li>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
let arr1 = [1, 2, 3].map(function (item, index) { return 2; }); console.log(arr1); // [2,2,2] let arr2 = [1, 2, 3].map(function (item, index) { return item *= 3; }); console.log(arr2); // [3,6,9] let arr3 = [1, 2, 3].map(function (item, index) { return `<li>${item}</li>`; }); console.log(arr3); // [ '<li>1</li>', '<li>2</li>', '<li>3</li>' ] console.log(arr3.join('')); // <li>1</li><li>2</li><li>3</li> |
join() 方法用于把数组中的所有元素放入一个字符串。 是ES6中的模板字符串,遇到变量用${ } 取值。 filter一般用于删除数组中的某一项。而map一般用于把这个数组修改一下。 10、includes includes 数组是否包含的意思,返回的是boolean includes 是ES6的方法 some、every 是ES5的方法 例子:
1 2 3 4 5 |
let arr3 = [1,2,3,4,55]; console.log(arr3.includes(5)); // false |
11、find find 是找出数组中的某一项。 返回找到的那一项。 […]
View Details