jquery Array processing Rollup

Source: Internet
Author: User

I haven't written anything for a while, and I'm going to summarize the more commonly used array processing methods in jquery.

$.each (Array, [callback]) traversal, very common
12345678 vararr = [‘javascript‘, ‘php‘, ‘java‘, ‘c++‘, ‘c#‘, ‘perl‘, ‘vb‘, ‘html‘, ‘css‘, ‘objective-c‘];$.each(arr, function(key, val) {    // firebug console    console.log(‘index in arr:‘+ key + ", corresponding value:"+ val);    // 如果想退出循环    // return false;});
$.grep (Array, callback, [invert]) filter, commonly used
123456789 var temp = [];temp = $.grep(arr, function(val, key) {    if(val.indexOf(‘c‘) != -1)         returntrue;    // 如果[invert]参数不给或为false, $.grep只收集回调函数返回true的数组元素    // 反之[invert]参数为true, $.grep收集回调函数返回false的数组元素}, false);console.dir(temp);
$.map (Array, [callback]) does not use too much
1234567891011121314 //1.6之前的版本只支持数组temp = $.map(arr, function(val, key) {    //返回null,返回的数组长度减1    if(val === ‘vb‘) returnnull;    returnval;});console.dir(temp);//1.6开始支持json格式的objectvarobj = {key1: ‘val1‘, key2: ‘val2‘, key3: ‘val3‘};temp = $.map(obj, function(val, key) {    returnval;});console.dir(temp);
$.inarray (val, array) determines whether in the specified array, the common
123 //返回元素在数组中的位置,0为起始位置,返回-1则未找到该元素console.log($.inArray(‘javascript‘, arr));
$.merge (first, second) merges two arrays, using frequency generally
123456789 varfrontEnd = [‘javascript‘, ‘css‘, ‘html‘],      backEnd = [‘java‘, ‘php‘, ‘c++‘];// 这种方式会修改第一个参数, 即frontEnd数组temp = $.merge(frontEnd, backEnd);console.dir(temp);console.dir(frontEnd);// 可以用下面的方式来避免对原数组的影响// $.merge($.merge([], frontEnd), backEnd);
$.unique (array) filters repeating elements in an array, not commonly used
123456789 //$.unique supports only DOM element arrays, removes duplicate DOM elements, and does not support other types of arrays (string or number) //get the original Dom array, not the jquery encapsulated var  divs = $ ( ' div ' //add several div-classes as DUP divs = Divs.concat ($ ( ' div.dup ' console.log (  + divs.length); divs = $.unique (divs); console.log (  < Code class= "JavaScript plain" >+ divs.length);
$.makearray (obj) to convert class array objects to arrays, not commonly used
12345 //首先什么是类数组对象?jQuery官网上用divs = getElementsByTag(‘div‘)来做例子//这个divs有类似数组的一些方法比如length,通过[index]方式获取元素等//然后通过$.makeArray(divs)使它转为数组,就可以用数组的其他功能//比如reverse(), pop()等
$ (DOM). ToArray () Restores the jquery collection to a DOM array, not commonly used
12 //跟makeArray一样,相当的不常用,一般情况可以忽略

jquery Array processing Rollup

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.