标签:
一.字符串操作
1.大小写
var s = "hello"; undefined g = s.toUpperCase(); "HELLO" g; "HELLO" g.toLowerCase(); "hello"
2.索引/截断
s.indexOf(‘o‘); 4 all.js:1 loading comments... var l = "hello world"; undefined l.substring(0,5); "hello" l.substring(7); "orld"
二.数组
1.索引/长度
var arr = [1, 2, 3.14, ‘Hello‘, null, true]; undefined arr.length; 6 arr[2]=99; 99 arr; [1, 2, 99, "Hello", null, true]
2.切片
arr; [1, 2, 99, "Hello", null, true] arr.slice(1,3); [2, 99]
3.pop/push与shift/unshift(从头删除,从头插入)
arr; [1, 2, 99, "Hello", null, true] arr.pop(); true arr; [1, 2, 99, "Hello", null] arr.push(1,"sd"); 7 arr; [1, 2, 99, "Hello", null, 1, "sd"] arr.unshift(‘head‘,‘sec‘); 9 arr; ["head", "sec", 1, 2, 99, "Hello", null, 1, "sd"] arr.shift(); "head" arr; ["sec", 1, 2, 99, "Hello", null, 1, "sd"]
4.排序/翻转/指定索引删除
var arr = [1, 2, 3.14, ‘Hello‘, null, true]; undefined arr.sort(); [1, 2, 3.14, "Hello", null, true] arr.reverse(); [true, null, "Hello", 3.14, 2, 1] var arr = [‘Microsoft‘, ‘Apple‘, ‘Yahoo‘, ‘AOL‘, ‘Excite‘, ‘Oracle‘]; undefined arr.splice(2, 3, ‘Google‘, ‘Facebook‘); ["Yahoo", "AOL", "Excite"] arr; ["Microsoft", "Apple", "Google", "Facebook", "Oracle"] arr.splice(2,2); ["Google", "Facebook"]
5.数组的拼接
arr; ["Microsoft", "Apple", "Oracle"] var lst = [‘A‘,‘B‘,‘C‘]; undefined var lstadd = arr.concat(lst); undefined lstadd; ["Microsoft", "Apple", "Oracle", "A", "B", "C"] lstadd.join(‘-‘); "Microsoft-Apple-Oracle-A-B-C"
6.多维数组
三.对象
var xiaoming = { name: ‘小明‘, birth: 1990, school: ‘No.1 Middle School‘, height: 1.70, weight: 65, score: null }; undefined xiaoming.name; "小明" xiaoming[‘name‘]; "小明" ‘name‘ in xiaoming; true xiaoming.hasOwnProperty(‘name‘); true
四.循环
1.数组
var arr = [‘Apple‘, ‘Google‘, ‘Microsoft‘]; for (var i = 0; i <= arr.length; i++) { alert(arr[i]); }
2.列表
var arr = ‘sdadda‘; for (var i = 0; i <= arr.length; i++) { alert(arr[i]); }
3.对象(类似python的dict)
var o = { name: "jack", age: 20, city: ‘Beijing‘ }; for (var key in o){ alert(o[key]); }
五.Map/set(Map:类似python的dict)
1.Map取值
var m = new Map([ [‘Michael‘, 95], [‘Bob‘, 75], [‘Tracy‘, 85] ]); alert(m.get(‘Michael‘));
2.Map增/删值
var m = new Map(); m.set(‘alex‘,67); m.set(‘bob‘,59); alert(m.has(‘bob‘)); m.delete(‘bob‘); alert(m.get(‘alex‘));
3.Set(去重,类似python列表的newlst = set(lst))
var s2 = new Set([1, 2, 3]); undefined s2; Set {1, 2, 3} s2.add(4); Set {1, 2, 3, 4} s2.delete(2); true s2; Set {1, 3, 4} all.js:1 loading comments... s2.add(4); Set {1, 3, 4}
六.迭代器(Array
、Map
和Set
都属于iterable
类型,用for..of迭代---PS:for..of是对for..in的修复)
1.for..of
var a = [‘A‘,‘B‘,‘C‘]; var s = new Set([‘A‘, ‘B‘, ‘C‘]); var m = new Map([[1,‘x‘],[‘name‘,‘y‘],[3,‘z‘]]); for (var x of a){ alert(x); } for (var x of s){ alert(x); } for (var x of m){ alert(x[0]+‘=‘+x[1]); }
2.forEach
//array var a = [‘A‘, ‘B‘, ‘C‘]; a.forEach(function (element, index, array) { // element: 指向当前元素的值 // index: 指向当前索引 // array: 指向Array对象本身 alert(element); }); //Map var m = new Map([[1, ‘x‘], [2, ‘y‘], [3, ‘z‘]]); m.forEach(function (value,key,map){ alert(value); alert(key); }); //set无索引 var s = new Set([‘A‘, ‘B‘, ‘C‘]); s.forEach(function (element, set) { alert(element); });
标签:
原文地址:http://www.cnblogs.com/alexkn/p/4764864.html