jquery inArray makearray Merge unique array function operation instance

Source: Internet
Author: User
Tags array length arrays constructor

jquery inarray makearray Merge unique array function operation instance

$.inarray (Val,array) determines whether the value exists in an array [common]
Explanation: Determines the position of the first parameter in the array, counting from 0 (if not found, returns-1).
Remember the indexof () method? IndexOf () returns the first occurrence of the string, and $.inarray () returns the position of the passed-in argument in the array, and, if found, returns a value greater than or equal to 0, or 1 if not found. Now, you know how to use it. With it, it becomes easy to judge whether a value exists in an array.

var _exist=$.inarray (' Mozi ', _mozi);
var _inexistence=$.inarray (' Wei ', _mozi)
if (_exist>=0) {
Alert (' Mozi exists in the array _mozi, whose index value in the array is: ' +_exist ');
}
if (_inexistence<0) {
Alert (' Wei does not exist in the array _mozi!, the return value is: ' +_inexistence+ '! ');
}$.merge (First,second) Merge two arrays [general]
Interpretation: The returned result modifies the contents of the first array-the elements of the first array followed by the elements of the second array.
This approach is to use jquery to replace the native concat () method, but the functionality is not concat () powerful, concat () can merge multiple arrays at the same time

Native concat () may be simpler than that.
_mozinew=$.merge (_mozi,[' ghost millet ', ' Shang Yang ', ' bin ', ' Pangjuan ', ' Su-Qin ', ' Zhang Yi '])
Alert (' The new array length after merging is: ' +_mozinew.length+ '. Its value is: ' +_mozinew ');

$.unique (array) filters repeating elements in an array [not common]
Explanation: Deletes the repeating element in the array. Handles only the delete DOM element array, not the string or numeric array.
See this method for the first time, think this is a very convenient method, can filter repetition, ha, how perfect, but look carefully, only processing DOM element. function 80 percent. So, I've defined it as a less common element, at least, I haven't used it since jquery.
var _h2arr=$.makearray (h2obj);
Repeat the array _h2arr once
_h2arr=$.merge (_h2arr,_h2arr);
var _curlen=_h2arr.length;
_h2arr=$.unique (_h2arr);
var _newlen=_h2arr.length;
Alert (' array _h2arr original length value: ' +_curlen+ ', filtered as: ' +_newlen
+ '. Co-filtration ' + (_curlen-_newlen) + ' repeat element ')


$.makearray (obj) converts class array objects to arrays [not common]
Explanation: Converts an array object of a class to an array object with a length property and a member index of 0 to Length-1.
This is a redundant method, and an omnipotent $ would have included this feature. The jquery official online explanation is very vague. Instead, it converts an array object of a class, such as a collection of element objects obtained with getElementsByTagName, to an arrays of objects.

var _makearr=$.makearray (h2obj);
Alert (the data type of the ' H2 element object collection is converted to: ' +_makearr.constructor.name ');/output Array

$ (DOM). ToArray () Restores all DOM elements to an array [not common]
Explanation: Restores all DOM elements in the jquery collection to an array;
The method is not commonly used, and the individual even feels that it is as superfluous as $.makearray.

Var _toarr=$ (' H2 '). ToArray ();
Alert (the data type after recovery of the ' H2 element collection is: ' +_toarr.constructor.name ');

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.