String方法:
var str = "abcabc";
console.log(str.length); //返回字符串长度,属性 6
console.log(str.indexOf("b")); //返回字符串出现的第一个字符的位置,否则返回-1 1
console.log(str.lastIndexOf("b")); //从右边开始字符串出现的第一个位置 4
console.log(str.search("bc")); //1
console.log(str.slice(2, 3)); //从第一个参数开始到第二个参数结束,不包含第二个参数索引的值c
console.log(str.substring(2, 3)); //无法接受负的索引值跟slice相似 c
console.log(str.substr(2, 3)); //从第一个参数索引起往后的位数 cab
console.log(str.replace("a", "b")); //将第一个出现的a替换 bbcabc
console.log(str.toUpperCase()); //转大写ABCABC
console.log(str.toLowerCase()); //转小写abcabc
var str1 = "d";
console.log(str.concat(" ", str1)); //拼接字符串用第一个参数 abcabc d
var str2 = " e ";
console.log(str1.trim()); //去掉前后空格’e’
console.log(str.charAt(3)); //返回索引所对应的值a
console.log(str.charCodeAt(3)); //返回索引所对应的值的code编码 97
Number方法:
var x = 123.0;
console.log(x.toString()); //123
console.log(x.toExponential(4)); //指定小数点后的指数计数法 //1.2300e+2
console.log(x.toFixed(5)); //指定位数小数的数字 123.00000
console.log(x.toPrecision(5)); //指定长度的数字 123.00
console.log(x.valueOf()); //数值返回数值 123
console.log(parseInt("1.23")); //解析一段字符串只返回首个整数 1
console.log(parseFloat("1.02 f")); //解析一段字符串只返回首个小数 1.02
console.log(Number.MAX_VALUE); //1.7976931348623157e+308
console.log(Number.MIN_VALUE); //5e-324
console.log(Number.NEGATIVE_INFINITY); //-Infinity
console.log(Number.POSITIVE_INFINITY); //Infinity
console.log(Number.NaN); //NaN
Array方法:
请避免使用new Array()去创建数组
//创建数组的方法
// 构造函数创建
var arr1 = new Array(6);
console.log(arr1); //[empty*6]
// 字面量创建
var arr2 = [];
arr2.length = 6;
console.log(arr2); //[empty*6]
// es6创建数组
var arr3 = Array.of(2);
console.log(arr3); //[2]
var arr4 = Array.of("abcd"); //["abcd"]
console.log(arr4);
//数组的判断方法
var a = [];
// 1.基于instanceof
a instanceof Array;
// 2.基于constructor
a.constructor === Array;
// 3.基于Object.prototype.isPrototypeOf
Array.prototype.isPrototypeOf(a);
// 4.基于getPrototypeOf
Object.getPrototypeOf(a) === Array.prototype;
// 5.基于Object.prototype.toString
Object.prototype.toString.apply(a) === '[object Array]';
//数组遍历方法
//回调函数可以传三个参数,值、索引、数组本身
//Array.foreach(callback)遍历数组的每一项,然后对每一项调用回调函数
a.forEach((value) => {
console.log(value * 2);
}); NaN NaN 8
//Array.map(callback)对每一项调用回调函数然后返回一个新数组
var map = a.map((value) => {
return value * 2;
})
console.log(map); // [NaN, NaN, 8]
//Array.filter(callback) 过滤器,返回符合条件的新数组
var num = [45, 4, 9, 16, 25];
var over18 = num.filter(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
console.log(over18); // [45, 25]
//Array.reduce(callback,initialValue) 可以进行累计,而不用创建变量
// initialValue(可选的初始值,作为第一次调用回调函数时传给 previousValue 的值)
var num1 = [45, 4, 9, 16, 25];
var sum = num1.reduce(myFunction);
function myFunction(total, value) {
return total + value;
}
console.log(sum); reduceRight方法 (和reduce的区别就是从后往前累计
var arr = [ {name: 'one'}, {name: 'two'}, {name: 'three'} ];
arr.reduce(function(prev, current, index, array){
if (index === 0){
return current.name;
} else if (index === array.length - 1){
return prev + ' & ' + current.name;
} else {
return prev + ', ' + current.name;
}
}, '');
// 返回结果 "one, two & three"
//Array.every(callback) 判断所有的值时是否符合条件,返回布尔值
var num2 = [45, 4, 9, 16, 25];
var allOver18 = num2.every(myFunction);
function myFunction(value) {
return value > 18;
}
console.log(allOver18); //false
//Array.some(callback) 判断是否有值符合判断条件
var num3 = [45, 4, 9, 16, 25];
var someOver18 = num3.some(myFunction);
function myFunction(value, index, array) {
return value > 18;
}
console.log(someOver18); //true
//
// //排序
res = a.sort((a, b) => {
return b - a;
})
console.log(res); //["d", 8, 7, 5, empty] 升序
res1 = a.sort((a, b) => {
return a - b;
})
console.log(res1); //["d", 5, 7, 8, empty] 降序
res2 = a.sort((a, b) => {
return 0.5 - Math.random();
})
console.log(res2); //随机排序,每次执行代码结果都不相同。
//数组对象排序
var cars = [{
type: "Volvo",
year: 2016
}, {
type: "Saab",
year: 2001
}, {
type: "BMW",
year: 2010
}];
cars.sort(function(a, b) {
var x = a.type.toLowerCase();
var y = b.type.toLowerCase();
if (x < y) {
return -1;
}
if (x > y) {
return 1;
}
return 0;
});
//es5会修改自身的方法
//a重新赋值
a = [1, 2, 3, 4]
//模拟栈
a.pop();
console.log(a); //[1,2,3]删除数组最以为元素
a.push('d');
console.log(a); //[1,2,3,'d']给数组末尾添加元素
//模拟队列
a.shift();
console.log(a); //[2,3,'d']删除数组第一位元素
a.unshift(1);
console.log(a); //[1,2,3,'d']给数组第一位添加元素
delete a[1];
console.log(a[1]); //undefind delete删除的元素会留下未定义的空洞
//splice的增删改查
a.splice(2, 0, 5, 6);
console.log(a); //[1,empty,5,6,3,'d']用splice添加元素
a.splice(3, 2, 7, 8); //返回一个新数组
console.log(a); //[1,empty,5,7,8,'d']用splice添元素
a.splice(0, 1);
console.log(a); //[empty,5,7,8,'d']用splice删除元素
//es5不会修改自身的方法
//concat方法,拼接数组
var array = [1, 2, 3];
var array2 = array.concat(4, [5, 6], [7, 8, 9]);
console.log(array2); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(array); // [1, 2, 3], 可见原数组并未被修改
// join方法 用指定字符拼接数组返回字符串
var array = ['We', 'are', 'Chinese'];
console.log(array.join()); // "We,are,Chinese"
console.log(array.join('+')); // "We+are+Chinese"
// slice方法 对数组进行切片返回新数组
var array = ["one", "two", "three", "four", "five"];
console.log(array.slice()); // ["one", "two", "three","four", "five"]
console.log(array.slice(2, 3)); // ["three"]
// toString方法
var array = ['Jan', 'Feb', 'Mar', 'Apr'];
var str = array.toString();
console.log(str); // Jan,Feb,Mar,Apr
// tolocalString方法
var array = [{
name: 'zz'
}, 123, "abc", new Date()];
var str = array.toLocaleString();
console.log(str); // [object Object],123,abc,2016/1/5 下午1:06:23
// indexOf方法 返回参数所在的位置
var array = ['abc', 'def', 'ghi', '123'];
console.log(array.indexOf('def')); // 1
//es6新增方法
//Array.of()
Array.of(8.0); // [8]
Array(8.0); // [empty × 8]
Array.of(8.0, 5); // [8, 5]
Array(8.0, 5); // [8, 5]
Array.of('8'); // ["8"]
Array('8'); // ["8"]
//Arrary.from(),
//第一个参数必须是数组,第二参数是映射函数,第三个是参数是指定第二参数内部的this
function translate() {
return Array.from(arguments, (value) => value + 1);
}
let numbers = translate(1, 2, 3);
console.log(numbers);
let helper = {
diff: 1,
add(value) {
return value + this.diff;
}
};
function translate() {
return Array.from(arguments, helper.add, helper);
}
let numbers = translate(1, 2, 3);
console.log(numbers); // 2,3,4
// indexOf\lastindexOf和find/findIndex都是查找数组元素
//indexOf局限于只能查询固定的元素,es6的方法可以根据条件判断
let numbers = [25, 30, 35, 40, 45];
console.log(numbers.find(n => n > 33)); // 35 返回大于33的元素
console.log(numbers.findIndex(n => n > 33)); // 2 返回大于33的元素的下标
//Array.fill() 用参数填充数组
let numbers = [1, 2, 3, 4];
numbers.fill(1);
console.log(numbers.toString()); // 1,1,1,1 一个参数时用该元素填充数组所有元素
let numbers = [1, 2, 3, 4];
numbers.fill(1, 2);
console.log(numbers.toString()); // 1,2,1,1 第二参数开始填充的位置
numbers.fill(0, 1, 3);
console.log(numbers.toString()); // 1,0,0,1 第三个参数是结束的位置
//如果填充的值是附数,那么会用该值与数组长度进行相加
//Array.copyWith() 也是填充数组
let numbers = [1, 2, 3, 4];
// 从索引 2 的位置开始粘贴
// 从数组索引 0 的位置开始复制数据
// 在遇到索引 1 时停止复制
numbers.copyWithin(2, 0, 1);
console.log(numbers.toString()); // 1,2,1,4
// Array.includes()
var array = [-0, 1, 2];
console.log(array.includes(+0)); // true
console.log(array.includes(1)); // true
var array = [NaN];
console.log(array.includes(NaN)); // true