Good taste: Array and string common operations

Source: Internet
Author: User
Tags array definition

Push (), pop (), Shift (), unshift (), splice (), join (), concat (), sort (), split (), parseint ()// Array----------------------------------------------------------------------------------------
var arr=[1,2,3];
var arr=new Array (n/a); Array definition

Arr.push (4); Rear Push-in 4
Alert (arr);

arr=[1,2,3];
Arr.unshift (4); Head pressed into 4

arr=[1,2,3];
Arr.pop (); Pop up the last one

Arr.shift (); pops up the first one
Alert (arr);

arr=[1,2,3,4,5,6,7,8];
Arr.splice (2,3); Intermediate Delete, Result: 1,2,6,7,8

arr=[1,2,3,4,5,6,7,8];
Arr.splice (5,0, ' A ', ' B ', ' C '); Intermediate INSERT, Result: 1,2,3,4,5,a,b,c,7,8

arr=[1,2,3,4,5,6,7,8];
Arr.splice (, ' A ', ' B '); Replacement, Result: 1,a,b,4,5,6,7,8

arr=[1,2,3,4];
arr.length=10;
Alert (arr[6]); Results: undefined

arr=[1,2,3,4];
Arr.join (': '); Results: 1:2:3:4


Character sorting
Vvar arr=[' float ', ' zindex ', ' xy ', ' absolute ', ' Blue ', ' Leo '];
Arr.sort (); Results: [' absolute ', ' blue ', ' float ', ' Leo ', ' xy ', ' zindex ']

Sort by number
var arr=[96, 8, 12, 72, 33, 118];
Arr.sort (function (num1,num2) {return num1-num2;}); Result: 8,12,33,72,96,118//array traversal var obj={a:5, B:3, c:98};
var attr= ';
for (attr in obj) {alert (attr+ ' = ' +obj[attr]);} Results pop up in turn: a=5,b=3,c=98


String--------------------------------------------------------------------------------------
var arr1=[1,2,3];
var arr2=[6,7,8];
Arr1.concat (ARR2); Array connection, Result: 1,2,3,6,7,8

var arr1= "SDSAFKASD";
var arr2= "55131535";
Arr1.concat (ARR2); string connection, Result: sdsafkasd55131535

var str= ' 12-4-5-8 ';
var arr=str.split ('-'); String split array, result: [12,4,5,8]

var str= ' 12 ';
parseint (str) +1; The string is converted to a number, the result: 13, otherwise 121

Good taste: Array and string common operations

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.