JS creates a new map object and operates on it

Source: Internet
Author: User

For map, many of the partners in the array may often confuse and use a vague concept, and I'll summarize my own experience here:

Array:

Generally we create this:

var arr = new Array ();

or direct: var arr = [];

Then operate on the array:

1, add elements:

var obj = ' The element you want to add ';

Arr.push (obj);

2, find the element:

var obj = ' You need to find the element ';

Arr.indexof (obj); The return value is-1, which means that obj does not exist in this array, and if not, returns the position of the element in the array, starting with 0.

3, delete the element:

Arr.pop ()//delete and return the last element

For example: var arr = [n/a]; var B = Arr.pop (); As a result, b = 3,arr = [+];

Arr.shift ()//delete and return the first element

Arr.splice (index,1)//delete the next 1 elements starting from the index position

var arr = [1,2,3,4,5];

For example: Arr.splice (2,2)//return [3,4],arr = [1,2,5]

Map creation:

var map = {};

To add an element:

For example, add: name = 1;

map[' name ']=1;

JS creates a new map object and operates on it

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.