jquery Filters the use of the grep, each, inArray, and map of the array and traverses the JSON object _jquery

Source: Internet
Author: User

Simply record the following several uses:

jquery grep () filter Traversal array

$ (). Ready (
  function () {
    var array = [1,2,3,4,5,6,7,8,9];
    var filterarray = $.grep (array,function (value) {return
      value > 5;//filter for greater than 5
    });
    for (Var i=0;i<filterarray.length;i++) {
      alert (filterarray[i]);
    }
    For (key in Filterarray) {
      alert (Filterarray[key]);
    }
);

jquery each () filter traversal array

$ (). Ready (
  function () {
    var anobject = {one:1,two:2,three:3};//to JSON array each
    $.each (anobject,function (Name,value) {
      alert (name);
      alert (value);
    });
    var anarray = [' One ', ' two ', ' three '];
    $.each (Anarray,function (n,value) {
      alert (n);
      alert (value);
    });

jquery InArray () filter Traversal array

$ (). Ready (
  function () {
    var anarray = [' One ', ' two ', ' three '];
    var index = $.inarray (' two ', anarray);
    Alert (index), returns the key value in the array, returns 1
    alert (Anarray[index]);//value is two
  }
);

jquery map () filter Traversal array

$ (). Ready (
  function () {
    var strings = [' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' S ', ' 6 '];
    var values = $.map (strings,function (value) {
        var result = new number (value);
        return isNaN (Result)? Null:result;//isnan:is not a number of the abbreviation
      }
    );
    For (key in values) {
      alert (Values[key]);
    }
);

jquery Traversal Parse JSON object 1:

var json = [{dd: ' SB ', AA: ' Dongdong ', RE1:123},{CCCC: ' DD ', LK: ' 1QW '}];
for (Var i=0,l=json.length;i<l;i++) {for
  (var key in Json[i]) {
    alert (key+ ': ' +json[i][key]);
  }

jquery Traversal Parse JSON object 2

There are JSON objects as follows:
var obj ={"name": "Feng Juan", "Password": "123456″," "Department": "Technical department", "Sex": "female", "old": ";
" Traversal method: For
(var p in obj) {
  str = str+obj[p]+ ', ';
  return str;
}

The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.