Array Assignment changes

Source: Internet
Author: User
Tags shallow copy

Tag: The result is the operation part of the cat start element specifying the description

1.var arr = ["One", "one", "three"];

var arrto = arr;
ARRTO[1] = "Test";
Document.writeln ("Original value of the array:" + arr + "<br/>");//export: Original value of the array: One,test,three
Document.writeln ("New value of the array:" + Arrto + "<br/>");//export: The new value of the array: One,test,three

Like the direct assignment above is a shallow copy, many times, this is not the result we want to get, in fact, we want the value of arr is not changed, right?

2.

For the slice function of the array object,
Returns a segment of an array. (still an array)
Arrayobj.slice (start, [end])
Parameters
Arrayobj
Required option. An Array object.
Start
Required option. The start element of the section specified in Arrayobj is the zero-based subscript.
End
Options are available. The end element of the part specified in the arrayobj is the zero-based subscript.
Description
The slice method returns an Array object that contains the specified portion of the arrayobj.
The slice method has been copied to the element specified by end, but does not include the element. If start is negative, it is processed as length + start, where length is the number of the array. If end is negative, it is treated as a length + end, where long is the number of the array. If End is omitted, then the slice method is copied to the end of arrayobj. If End appears before start, no elements are copied into the new array.

var arr = ["One", "one", "three"];

var Arrtoo = arr.slice (0);
ARRTOO[1] = "Set Map";
Document.writeln ("Original value of the array:" + arr + "<br/>");//export: Original value of the array: One,two,three
Document.writeln ("New value of the array:" + Arrtoo + "<br/>");//export: The new value of the array: One,set Map,three

The Concat () method is used to concatenate two or more arrays.
The method does not alter the existing array, but only returns a copy of the concatenated array.
Grammar
Arrayobject.concat (Arrayx,arrayx,......, Arrayx)
Description
Returns a new array. The array is generated by adding all the Arrayx parameters to the Arrayobject. If the argument for the concat () operation is an array, then the element in the array is added, not the array.
var arr = ["One", "one", "three"];

var arrtooo = Arr.concat ();
ARRTOOO[1] = "Set Map to";
Document.writeln ("Original value of the array:" + arr + "<br/>");//export: Original value of the array: One,two,three
Document.writeln ("New value of the array:" + arrtooo + "<br/>");//export: The new value of the array: One,set Map to,three

Array Assignment changes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.