Object common method, an object copy

  • string

method description
charAt() Returns the character at the specified location.
charCodeAt() Returns the Unicode character encoding in the location specified in.
concat() Connection string.
indexOf() Retrieval string.
match() Find one or more regular expression matching.
replace() Alternatively substring match the regular expression.
search() Retrieving the value of the regular expression to match.
slice() Extracting fragments of a string, and returns the extracted part in a new string.
split() String is divided into an array of strings.
toLocaleLowerCase() Converting a string to lowercase.
toLocaleUpperCase() String converted to uppercase.
toLowerCase() Converting a string to lowercase.
toUpperCase() String converted to uppercase.
substr() Specified number of characters from the start index extraction string.
substring() Between two character strings specified in the extracted index number.
  • Array

method Attributes
slice[start,end) Returns the start index to the end of the new array of items between index from the original array (does not affect the original array)
. 1 Parameters: n namely:. N to the end of all
. 2 parameters: [start, end]
splice():
. Delete: two parameters, the starting position, the number of items deleted
. Insert: 3, the start position, several deleted, inserted item
. Alternatively: any parameter, the starting position, several deleted, inserted into any number of items
pop() Remove the last element of the array, reducing the length of the array, the return value is deleted. (No-argument)
push() The last parameter is loaded into the array, returns the length of the new array. (Any parameter)
shift() Removes the first element of the array, the array length minus 1, the return value is deleted. (No-argument)
unshift() Add one or more elements to the beginning of an array and returns the new length. (Any parameter)
sort() Specified parameters to sort the array, the value is returned after a sorted array (nullary / functions)
concat(3,4) The two arrays are spliced ​​together. The return value is a copy (Any parameter)
join() The element groups of the array from a string, as the separator in the separator, the default is omitted, with Comma Separated Values
indexOf() Find backward from the beginning of the array, accepts two parameters, to find items (optional) and find the starting position of the index
lastIndexOf() 从数组末尾开始向前查找,接受两个参数,要查找的项(可选)和查找起点位置的索引
every() 对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true。
filter() 对数组中的每一项运行给定函数,返回该函数会返回true的项组成数组。
forEach() 对数组的每一项运行给定函数,这个方法没有返回值。
map() 对数组的每一项运行给定函数,返回每次函数调用的结果组成的数组。
some() 对数组的每一项运行给定参数,如果该函数对任一项返回true,则返回true。以上方法都不会修改数组中的包含的值。
reduce()和reduceRight() 缩小数组的方法,这两个方法都会迭代数组的所有项,然后构建一个最终返回的值。
  • Math

方法 描述
ceil(x) 尽可能取最大。
floor(x) 尽可能取最小。
round(x) 把数四舍五入为最接近的整数。
max(x,y) 返回 x 和 y 中的最高值。
min(x,y) 返回 x 和 y 中的最低值。
pow(x,y) 返回 x 的 y 次幂。
random() 返回 0 ~ 1 之间的随机数。
sqrt(x) 返回数的平方根。
  • 对象的深拷贝/浅拷贝

在javascript中的数据类型可以分为基础数据类型和引用数据类型,只有引用类型才会用到拷贝;

对象就是引用类型的数据;拷贝根据层次可以划分为深拷贝和浅拷贝两种;

浅拷贝只是对对象的各个属性进行一次复制,并不会进行递归复制;

简单的说,就是只会复制对象的第一层属性;深拷贝并不仅仅拷贝对象的第一层属性,而且会递归拷贝目标对象的所有属性

1 使用JSON.parse(JSON.stringify(obj))

会返回原对象的副本,可以进行深拷贝

let newObj = JSON.parse(JSON.stringify(obj));

注意:
当原对象中某个属性值为函数时,会将此属性丢失,拷贝后的对象中没有此属性,因此该方法不可以拷贝用户自定义的方法属性;
同时, 不能用于复制循环引用对象

2 使用ES6中的 Object.assign()

Object.assign() 方法用于将从一个或多个源对象中的所有可枚举的属性值复制到目标对象(即参数中的第一个对象),并返回合并后的对象.
可以复制循环引用对象

let objCopy = Object.assign({}, obj);

注意:
Object.assign 只是浅拷贝,只能复制第一层属性;

3 使用ES6中的展开操作符(…)

const array = ["a","c","d", {four: 4},];
const newArray = [...array];

注意:只对浅拷贝有效

4 使用递归进行深拷贝

function copy (obj) {
    var newobj = obj.constructor === Array ? [] : {};
    if(typeof obj !== 'object') return;   
    for(var i in obj){
        newobj[i] = typeof obj[i] === 'object' ? copy(obj[i]) : obj[i];
    }
    return newobj
}
var copyArray = copy(array)

注意:不适用于循环引用对象,对于循环引用对象,可以使用插件将循环解开.

总结:
(1 )JSON.parse(JSON.stringify(obj))适用于深拷贝属性中没有值为方法的对象,适用于非循环引用的对象;
(2) Object.assign()适用于浅拷贝对象,循环或者非循环都可以;
(3) 递归深拷贝适用于非循环引用的对象.

放到最后:关于数组的拷贝,修改后对原数组无影响

var array = [1, 2, 3, 4];
var newArray = array.slice();

var array = [1, 2, 3, 4];
var newArray = array.concat();

Guess you like

Origin blog.csdn.net/weixin_43931047/article/details/90896090