Translation The mutators in JavaScript

Source: Internet
Author: User

This article translates the JavaScript Tutorial playlist of up master Kudvenkat on YouTube

SOURCE Address here:

https://www.youtube.com/watch?v=PMsVM7rjupU&list=PL6n9fhu94yhUA99nOsJkKXBqokT3MBK0b

There are many ways in JavaScript that can be used on a sequence of objects. Some methods change the number of objects, while others do not, and those that change the sequence of objects are called mutator methods.

Here are some non-mutator methods:

Contains

IndexOf

LastIndexOf

The following are some of the mutator methods

Push

Pop

Shift

Unshift

Reverse

Sort

Splice

We discussed the push (), pop (), Shift (), and Unshift () methods in chapter 20. In this video we will discuss

Sort

Reverse

Splice

Javascript Sort method: Sorts the elements in a sequence. By default, the sort () method converts the values in the sequence into a string and compares the strings to achieve the order. But it's only good for string, That's not true for numbers. Let's see an example.

Example: Note that the string is perfectly sorted

var myArray = ["Sam", "Mark", "Tom", "David"];myarray.sort ();d ocument.write (MyArray);

Output:david,mark,sam,tom

Now, let's take a look at the example of sorting numbers

var myArray = [1, 2, 3];myarray.sort ();d ocument.write (MyArray);

output:1,10,2,20,3

Notice that the numbers don't sort as we expect. The way we solve this problem is to add a "comparison function" to the sort function as its argument. This comparison function should return a negative number, 0, or positive

Example:

var myArray = [1, 2, 3];myarray.sort (functionreturn A- B});d Ocument.wri Te (MyArray);

output:1,2,3,10,20

Now let's talk about how this comparison function works. This function has two parameters, (a, B) This function will be a A-

The result of B is returned. If the return value is

Positive number-A is larger than B

Negative number-A is smaller than b

0-Then a equals b

So, based on the returned value, the numbers in the sequence can be sorted.

Sort numbers in descending order: there are two ways

1. Change (A-B) to (B-A)

Example:

var myArray = [1, 2, 3];myarray.sort (functionreturn B- A});d Ocument.wri Te (MyArray);

2. After sorting in ascending order and then using the reverse () function to achieve the purpose of descending

Example:

var myArray = [1, 2, 3];myarray.sort (functionreturn A- B}). Reverse ();d O Cument.write (MyArray);

output:20,10,3,2,1

Javascript Reverse method: Reverse ordering elements in a sequence

Javascript Splice method: adding or reducing elements to a sequence

Format: Array.splice (index,deletecount,item1,.... ItemX)

Index-must specify where to add or remove elements

DeleteCount-Must specify how many elements to remove. If set to 0, no element is removed

Item1,....., ItemX-Non-mandatory, specifies the element to be added to the sequence

Example:

var myArray = [1,2,5];myarray.splice (2, 0, 3, 4);d ocument.write (MyArray);

output:1,2,3,4,5

Example:

var myArray = [1,2,55,67,3];myarray.splice (2, 2);d ocument.write (MyArray);

output:1,2,3

Translation The mutators in JavaScript

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.