标签:区别 修改 soft 有一个 删除 style sof targe tar
var arr = [1,2,3,4,5] console.log(arr.push(5)) // 6 console.log(arr) // [1,2,3,4,5,5]
var arr = [1,2,3,4,5] console.log(arr.unshift(2)) // 6 console.log(arr) //[2,1,2,3,4,5]
var arr = [1,2,3,4,5] console.log(arr.shift()) // 1 console.log(arr) // [2,3,4,5]
var arr = [1,2,3,4,5] console.log(arr.pop()) // 5 console.log(arr) //[1,2,3,4]
var arr = [1,2,3,4,5] console.log(arr.splice(2,2)) //[3,4] console.log(arr) // [1,2,5]
var arr = [1,2,3,4,5] console.log(arr.concat([1,2])) // [1,2,3,4,5,1,2] console.log(arr) // [1,2,3,4,5]
var str = ‘123456‘ console.log(str.split(‘‘)) // ["1", "2", "3", "4", "5", "6"]
var arr = [2,10,6,1,4,22,3] console.log(arr.sort()) // [1, 10, 2, 22, 3, 4, 6] var arr1 = arr.sort((a, b) =>a - b) console.log(arr1) // [1, 2, 3, 4, 6, 10, 22] var arr2 = arr.sort((a, b) =>b-a) console.log(arr2) // [22, 10, 6, 4, 3, 2, 1]
var arr = [1,2,3,4,5] console.log(arr.reverse()) // [5,4,3,2,1] console.log(arr) // [5,4,3,2,1]
let arr = [1,2,3,4,5] console.log(arr.slice(1,3)) // [2,3] console.log(arr) // [1,2,3,4,5]
11.arr.lastIndexOf() 和arr.indexOf()的功能一样,不同的是从后往前查找
var arr = [1,2,3,4,5,2] var arr1 = arr.lastIndexOf(2) console.log(arr1) // 5 let arr2 = arr.lastIndexOf(9) console.log(arr2) // -1
12.arr.join() 方法用于把数组中的所有元素放入一个字符串
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr.join()
)
13.arr.toSource() 方法表示对象的源代码
function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}
var bill=new employee("Bill Gates","Engineer",1985);
document.write(bill.toSource()
);
14.arr.toString() 方法可把数组转换为字符串,并返回结果
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr.toString()
)
15.arr.toLocaleString()把数组转换为本地字符串
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
document.write(arr.toLocaleString()
)
16.arr.valueOf() 方法返回 Array 对象的原始值。。
valueOf() 方法通常由 JavaScript 在后台自动调用,并不显式地出现在代码中
var arr = [1,2,3,4,5,2] var arr1 = arr.indexOf(2) console.log(arr1) // 1 var arr2 = arr.indexOf(9) console.log(arr2) // -1
var arr = [1,2,3,4,5] arr.forEach( (value,index,array)=>{ console.log(`value:${value} index:${index} array:${array}`) }) // value:1 index:0 array:1,2,3,4,5 // value:2 index:1 array:1,2,3,4,5 // value:3 index:2 array:1,2,3,4,5 // value:4 index:3 array:1,2,3,4,5 // value:5 index:4 array:1,2,3,4,5 var arr = [1,2,3,4,5] arr.forEach( (value,index,array)=>{ value = value * 2 console.log(`value:${value} index:${index} array:${array}`) }) console.log(arr) // value:2 index:0 array:1,2,3,4,5 // value:4 index:1 array:1,2,3,4,5 // value:6 index:2 array:1,2,3,4,5 // value:8 index:3 array:1,2,3,4,5 // value:10 index:4 array:1,2,3,4,5 // [1, 2, 3, 4, 5]
var arr = [1,2,3,4,5] arr.map( (value,index,array)=>{ value = value * 2 console.log(`value:${value} index:${index} array:${array}`) }) console.log(arr)
ps: arr.forEach()和arr.map()的区别
1. arr.forEach()是和for循环一样,是代替for。arr.map()是修改数组其中的数据,并返回新的数据。
2. arr.forEach() 没有return arr.map() 有return
var arr = [1,2,3,4,5] var arr1 = arr.filter( (i, v) => i < 3) console.log(arr1) // [1, 2]
var arr = [1,2,3,4,5] var arr1 = arr.every( (i, v) => i < 3) console.log(arr1) // false var arr2 = arr.every( (i, v) => i < 10) console.log(arr2) // true
var arr = [1,2,3,4,5] var arr1 = arr.some( (i, v) => i < 3) console.log(arr1) // true var arr2 = arr.some( (i, v) => i > 10) console.log(arr2) // false
var arr = [0,1,2,3,4] var arr1 = arr.reduce((preValue, curValue) => preValue + curValue ) console.log(arr1) // 10
var arr2 = arr.reduce((preValue,curValue)=>preValue + curValue,5) console.log(arr2) // 15
var arr = [0,1,2,3,4] var arr1 = arr.reduceRight((preValue, curValue) => preValue + curValue ) console.log(arr1) // 10
var str = ‘12345‘ console.log(Array.from(str)) // ["1", "2", "3", "4", "5"] vsr obj = {0:‘a‘,1:‘b‘,length:2} console.log(Array.from(obj)) // ["a", "b"]
var str = ‘11‘ console.log(Array.of(str)) // [‘11‘]
console.log(new Array(‘11‘)) // [‘11]
console.log(new Array(2)) //[empty × 2] 是个空数组 console.log(Array.of(2)) // [2]
var arr = [1,2,3,4,5,6,7] var arr1 = arr.copyWithin(1) console.log(arr1) // [1, 1, 2, 3, 4, 5, 6] var arr2 = arr.copyWithin(1,2) console.log(arr2) // [1, 3, 4, 5, 6, 7, 7] var arr3 = arr.copyWithin(1,2,4) console.log(arr3) // [1, 3, 4, 4, 5, 6, 7]
var arr = [1,2,3,4,5,2,4] var arr1 = arr.find((value, index, array) =>value > 2) console.log(arr1) // 3
var arr = [1,2,3,4,5] var arr1 = arr.findIndex((value, index, array) => value > 3) console.log(arr1) // 3
var arr = [1,2,3,4,5] var arr1 = arr.includes(2) console.log(arr1) // ture var arr2 = arr.includes(9) console.log(arr2) // false var arr3 = [1,2,3,NaN].includes(NaN) console.log(arr3) // true
var arr = [1,2,3,4] var arr2 = arr.keys() for (var key of arr2) { console.log(key); // 0,1,2,3 }
var arr = [1,2,3,4] var arr1 = arr.values() for (var val of arr1) { console.log(val); // 1,2,3,4 }
var arr = [1,2,3,4] var arr1 = arr.entries() for (var e of arr1) { console.log(e); // [0,1] [1,2] [2,3] [3,4] }
标签:区别 修改 soft 有一个 删除 style sof targe tar
原文地址:https://www.cnblogs.com/li923/p/11431216.html